public Set<Integer> findDuplicates(List<Integer> listContainingDuplicates)
{
final Set<Integer> setToReturn = new HashSet();
final Set<Integer> set1 = new HashSet();
for (Integer yourInt : listContainingDuplicates)
{
if (!set1.add(yourInt))
{
setToReturn.add(yourInt);
}
}
return setToReturn;
}
have a List of type Integer eg:
[1, 1, 2, 3, 3, 3]
I would like a method to return all the duplicates eg:[1, 3]
----------------
List<Item> list = ...; list.removeAll(new HashSet<Item>(list)); return new HashSet<Item>(list);
----------------
public static Set<Integer> findDuplicates(List<Integer> input) { List<Integer> copy = new ArrayList<Integer>(input); for (Integer value : new HashSet<Integer>(input)) { copy.remove(value); } return new HashSet<Integer>(copy ); }
----------------
public static void main(String[] args) { List<Integer> list = new LinkedList<Integer>(); list.add(1); list.add(1); list.add(1); list.add(2); list.add(3); list.add(3); Map<Integer,Integer> map = new HashMap<Integer, Integer>(); for (Integer x : list) { Integer val = map.get(x); if (val == null) { map.put(x,1); } else { map.remove(x); map.put(x,val+1); } } List<Integer> result = new LinkedList<Integer>(); for (Entry<Integer, Integer> entry : map.entrySet()) { if (entry.getValue() > 1) { result.add(entry.getKey()); } } for (Integer x : result) { System.out.println(x); } }
----------------
public void testFindDuplicates() {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(3);
Set<Integer> result = new HashSet<Integer>();
int currentIndex = 0;
for (Integer i : list) {
if (!result.contains(i) && list.subList(currentIndex + 1, list.size()).contains(i)) {
result.add(i);
}
currentIndex++;
}
assertEquals(2, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(3));
}
-----------
ublic List<int> GetDuplicates(int max) { //allocate and clear memory to 0/false bit[] buckets=new bit[max] memcpy(buckets,0,max); //find duplicates List<int> result=new List<int>(); foreach(int val in List) { if (buckets[val]) { result.add(value); } else { buckets[val]=1; } } return result }
----------
private <T> Set<T> findDuplicates(Collection<T> list) {
Set<T> duplicates = new LinkedHashSet<T>();
Set<T> uniques = new HashSet<T>();
for(T t : list) {
if(!uniques.add(t)) {
duplicates.add(t);
}
}
return duplicates;
}
-------
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class CrunchifyFindDuplicateInList {
/**
* @author Crunchify.com
*/
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
for (int i = 0; i < 10; i++) {
list.add(String.valueOf(i));
}
for (int i = 0; i < 5; i++) {
list.add(String.valueOf(i));
}
System.out.println("My List : " + list);
System.out.println("\nHere are the duplicate elements from list : " + findDuplicates(list));
}
public static Set<String> findDuplicates(List<String> listContainingDuplicates) {
final Set<String> setToReturn = new HashSet<String>();
final Set<String> set1 = new HashSet<String>();
for (String yourInt : listContainingDuplicates) {
if (!set1.add(yourInt)) {
setToReturn.add(yourInt);
}
}
return setToReturn;
}
}
1
2
3
|
My List : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
Here are the duplicate elements from list : [3, 2, 1, 0, 4]
|
No comments:
Post a Comment