Java- Print all duplicate elements from a List
In Java, we have multiple ways to find duplicate elements in a List. We can loop through the List and add each element to a Set which stores the only unique elements.
1. Filter & Set.add()
Using filter(Predicate<? super T> predicate) method of Stream API and adding each element to Set will give us the duplicate element.
// List contains duplicate elements
List<Integer> list = Arrays.asList(8, 49, 25, 98, 98, 32, 15, 15,
10, 30, 20, 20, 50, 10, 12, 10
);
Set<Integer> set = new HashSet<>();
Set<Integer> output1 = list.stream()
.filter(e -> !set.add(e))
.collect(Collectors.toSet());
output1.forEach(System.out::println); // 98,20,10,152. Collections.frequency()
Collections.frequency(Collection<?> c, Object o) returns the number of elements in the specified collection equal to the specified object. And its return type is int.
// List contains duplicate elements
List<Integer> list = Arrays.asList(8, 49, 25, 98, 98, 32, 15, 15,
10, 30, 20, 20, 50, 10, 12, 10
);
Set<Integer> output2 = list.stream()
.filter(e -> Collections.frequency(list, e) > 1)
.collect(Collectors.toSet());
output2.forEach(System.out::println); // 98,20,10,153. Collectors.groupingBy()
Collectors.groupingBy() provide us with functionality similar to the ‘GROUP BY’ clause in the SQL language. We use them for grouping objects by some property and storing results in a Map instance.
// List contains duplicate elements
List list = Arrays.asList(8, 49, 25, 98, 98, 32, 15, 15,
10, 30, 20, 20, 50, 10, 12, 10
);
Set output3 = list.stream()
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.entrySet()
.stream()
.filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
output3.forEach(System.out::println); // 98,20,10,15Check out the complete example.
DuplicateElements.java
package org.websparrow;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class DuplicateElements {
public static void main(String[] args) {
// List contains duplicate elements
List list = Arrays.asList(8, 49, 25, 98, 98, 32, 15, 10,
30, 20, 20, 50, 10, 12, 10, 15);
System.out.println("Result using filter and add");
Set output1 = filterAndAdd(list);
output1.forEach(System.out::println);
System.out.println("Result using Collections.frequency");
Set output2 = collectionsFrequency(list);
output2.forEach(System.out::println);
System.out.println("Result using Collectors.groupingBy");
Set output3 = collectorsGroupingBy(list);
output3.forEach(System.out::println);
}
private static Set filterAndAdd(List list) {
Set set = new HashSet<>();
return list.stream().filter(e -> !set.add(e))
.collect(Collectors.toSet());
}
private static Set collectionsFrequency(List list) {
return list.stream().filter(e -> Collections.frequency(list, e) > 1)
.collect(Collectors.toSet());
}
private static Set collectorsGroupingBy(List list) {
return list.stream().collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.entrySet()
.stream().filter(e -> e.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
}Output
Result using filter and add
98
20
10
15
Result using Collections.frequency
98
20
10
15
Result using Collectors.groupingBy
98
20
10
15References
- How to count the frequency of a character in a string in Java
- Java 8 Stream filter() Method Example
- How to find distinct elements in a list in Java