For example my list contains {4, 6, 6, 7, 7, 8} and I want final result = {6, 6, 7, 7}
One way is to loop through the list and eliminate unique values (4, 8 in this
Your List
should ideally have been a Set
which doesn't allow duplicates in the first place. As an alternative to looping, you could either convert and switch to Set
or use it intermediately to eliminate duplicates as follows:
List<Long> dupesList = Arrays.asList(4L, 6L, 6L, 7L, 7L, 8L);
Set<Long> noDupesSet = new HashSet<Long>(dupesList);
System.out.println(noDupesSet); // prints: [4, 6, 7, 8]
// To convert back to List
Long[] noDupesArr = noDupesSet.toArray(new Long[noDupesSet.size()]);
List<Long> noDupesList = Arrays.asList(noDupesArr);
System.out.println(noDupesList); // prints: [4, 6, 7, 8]
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class FindDuplicate {
public static void main(String[] args) {
// Load all your ArrayList
List<String> list = new ArrayList<String>();
list.add("Jhon");
list.add("Jency");
list.add("Mike");
list.add("Dmitri");
list.add("Mike");
// Set will not allow duplicates
Set<String> checkDuplicates = new HashSet<String>();
System.out.println("Actual list " + list);
for (int i = 0; i < list.size(); i++) {
String items = list.get(i);
if (!checkDuplicates.add(items)) {
// retain the item from set interface
System.out.println("Duplicate in that list " + items);
}
}
}
}
Some good answers so far but another option just for the fun of it. Loop through the list trying to place each number into a Set e.g. a HashSet. If the add method returns false you know the number is a duplicate and should go into the duplicate list.
EDIT: Something like this should do it
Set<Number> unique = new HashSet<>();
List<Number> duplicates = new ArrayList<>();
for( Number n : inputList ) {
if( !unique.add( n ) ) {
duplicates.add( n );
}
}
With Guava and Java 8, it's trivial and fast:
Multiset<Integer> multiset = HashMultiset.create(list);
return list.stream()
.filter(i -> multiset.count(i) > 1)
.collect(Collectors.toList());
The first line computes the counts using a sort of a hash map. The remainder is more than obvious.
Something like this could simulate the multiset:
HashMap<Integer, Integer> multiset = new HashMap<>();
list.stream().forEach(i ->
multiset.compute(i, (ignored, old) -> old==null ? 1 : old+1)));
The following will work with Eclipse Collections:
IntBag bag = IntLists.mutable.with(4, 6, 6, 7, 7, 8).toBag().selectDuplicates();
If you want boxed values instead of primitives, the following will work:
Bag<Integer> bag = Lists.mutable.with(4, 6, 6, 7, 7, 8).toBag().selectDuplicates();
Note: I am a committer for Eclipse Collections.
List<Number> inputList = Arrays.asList(4, 6, 6, 7, 7, 8);
List<Number> result = new ArrayList<Number>();
for(Number num : inputList) {
if(Collections.frequency(inputList, num) > 1) {
result.add(num);
}
}
I am not sure about the efficiency, but I find the code easy to read (and that should be preferred.
EDIT: changing Lists.newArrayList()
to new ArrayList<Number>();