Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java.
Examples:
Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34}
Output: [59, 13]
Explanation:
The only duplicate elements in the given stream are 59 and 13.Input: Stream = {5, 13, 4, 21, 27, 2, 59, 34}
Output: []
Explanation:
There are no duplicate elements in the given stream, hence the output is empty.
There are many methods to find duplicate elements in a Stream:
- Using Set: Since Set has the property that it cannot contain any duplicate element. So if we add the elements in a Set, it automatically discards the duplicate elements while addition itself.
Approach:
- Get the stream of elements in which the duplicates are to be found.
- Traverse each element of the stream
- For each element in the stream, if it is not present in the set, add it. This can be done using Set.add() method.
Set.add()
- If the element is present in the Set already, then this Set.add() returns false.
- Hence we can print such elements or collect them for further process. We will print these elements in this case.
Below is the implementation of the above approach:
Example:
// Java program to find the duplicate// elements in a Stream using SetÂÂimportjava.util.*;importjava.util.stream.*;ÂÂpublicclassGfG {   Â// Function to find the   Â// duplicates in a Stream   Âpublicstatic<T> Set<T>   ÂfindDuplicateInStream(Stream<T> stream)   Â{       Â// Set to store the duplicate elements       ÂSet<T> items =newHashSet<>();       Â// Return the set of duplicate elements       Âreturnstream           Â// Set.add() returns false           Â// if the element was           Â// already present in the set.           Â// Hence filter such elements           Â.filter(n -> !items.add(n))           Â// Collect duplicate elements           Â// in the set           Â.collect(Collectors.toSet());   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Initial stream       ÂStream<Integer> stream           Â= Stream.of(5,13,4,                       Â21,13,27,                       Â2,59,59,34);       Â// Print the found duplicate elements       ÂSystem.out.println(           ÂfindDuplicateInStream(stream));   Â}}Output:[59, 13]
- Using Collectors.groupingBy(): The groupingBy() method of Collectors class in Java groups the objects by some property. So we will pass the property of redundancy and collect the result in a Set.
Approach:
- Get the stream of elements in which the duplicates are to be found.
- Traverse each element of the stream
- For each element in the stream, group them along with their frequency in a map, using Collectors.groupingBy() method.
stream.collect( Collectors.groupingBy(Function.identity(), Collectors.counting())); - Then for each element in the collected map, if the frequency of any element is more than one, then this element is a duplicate element.
- Hence we can print such elements or collect them for further process. We will print these elements in this case.
Below is the implementation of the above approach:
Example:
// Java program to find the duplicate// elements in a Stream using Collectors.groupingBy()ÂÂimportjava.util.*;importjava.util.stream.*;importjava.util.function.Function;ÂÂpublicclassGfG {   Â// Function to find the   Â// duplicates in a Stream   Âpublicstatic<T> Set<T>   ÂfindDuplicateInStream(Stream<T> stream)   Â{       Â// Return the set of duplicate elements       Âreturnstream           Â// Group the elements along           Â// with their frequency in a map           Â.collect(               ÂCollectors.groupingBy(                   ÂFunction.identity(),                   ÂCollectors.counting()))           Â// Convert this map into a stream           Â.entrySet()           Â.stream()           Â// Check if frequency > 1           Â// for duplicate elements           Â.filter(m -> m.getValue() >1)           Â// Find such elements           Â.map(Map.Entry::getKey)           Â// And Collect them in a Set           Â.collect(Collectors.toSet());   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Initial stream       ÂStream<Integer> stream           Â= Stream.of(5,13,4,                       Â21,13,27,                       Â2,59,59,34);       Â// Print the found duplicate elements       ÂSystem.out.println(           ÂfindDuplicateInStream(stream));   Â}}Output:[59, 13]
- Using Collections.frequency(): The frequency() method of Collections class in Java, counts the frequency of the specified element in the given list. So we will then find out the elements that have frequency more than 1, which are the duplicate elements.
Approach:
- Get the stream of elements in which the duplicates are to be found.
- Traverse each element of the stream
- For each element in the stream, count the frequency of each element, using Collections.frequency() method.
Collections.frequency(list, i)
- Then for each element in the collection list, if the frequency of any element is more than one, then this element is a duplicate element.
- Hence we can print such elements or collect them for further process. We will print these elements in this case.
Below is the implementation of the above approach:
Example:
// Java program to find the duplicate// elements in a Stream// using Collections.frequency()ÂÂimportjava.util.*;importjava.util.stream.*;ÂÂpublicclassGfG {   Â// Function to find the   Â// duplicates in a Stream   Âpublicstatic<T> Set<T>   ÂfindDuplicateInStream(List<T> list)   Â{       Â// Return the set of duplicate elements       Âreturn           Â// Get the stream from the list           Âlist.stream()               Â// Count the frequency of each element               Â// and filter the elements               Â// with frequency > 1               Â.filter(i -> Collections.frequency(list, i) >1)               Â// And Collect them in a Set               Â.collect(Collectors.toSet());   Â}   Â// Driver code   Âpublicstaticvoidmain(String[] args)   Â{       Â// Initial stream       ÂList<Integer> list           Â= Arrays.asList(5,13,4,                           Â21,13,27,                           Â2,59,59,34);       Â// Print the found duplicate elements       ÂSystem.out.println(           ÂfindDuplicateInStream(list));   Â}}Output:[59, 13]
