Skip to content

Instantly share code, notes, and snippets.

@dallaslu
Last active January 8, 2017 17:09
Show Gist options
  • Save dallaslu/acf4d9258f1822228b27f959a85724bd to your computer and use it in GitHub Desktop.
Save dallaslu/acf4d9258f1822228b27f959a85724bd to your computer and use it in GitHub Desktop.
Get Duplicate Elements https://www.v2ex.com/t/333143
package com.v2ex.utils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class DuplicateElementUtils{
public static <T> List<T> getDuplicateElements(List<T> list){
List<T> result = new ArrayList<T>();
Set<T> set = new HashSet<T>();
for(T e : list){
if(!set.add(e)){
result.add(e);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment