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:
import java.util.*;
import java.util.stream.*;
public class GfG {
public static <T> Set<T>
findDuplicateInStream(Stream<T> stream)
{
Set<T> items = new HashSet<>();
return stream
.filter(n -> !items.add(n))
.collect(Collectors.toSet());
}
public static void main(String[] args)
{
Stream<Integer> stream
= Stream.of( 5 , 13 , 4 ,
21 , 13 , 27 ,
2 , 59 , 59 , 34 );
System.out.println(
findDuplicateInStream(stream));
}
}
|
- 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:
Below is the implementation of the above approach:
Example:
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
public class GfG {
public static <T> Set<T>
findDuplicateInStream(Stream<T> stream)
{
return stream
.collect(
Collectors.groupingBy(
Function.identity(),
Collectors.counting()))
.entrySet()
.stream()
.filter(m -> m.getValue() > 1 )
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
public static void main(String[] args)
{
Stream<Integer> stream
= Stream.of( 5 , 13 , 4 ,
21 , 13 , 27 ,
2 , 59 , 59 , 34 );
System.out.println(
findDuplicateInStream(stream));
}
}
|
- 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:
Below is the implementation of the above approach:
Example:
import java.util.*;
import java.util.stream.*;
public class GfG {
public static <T> Set<T>
findDuplicateInStream(List<T> list)
{
return
list.stream()
.filter(i -> Collections.frequency(list, i) > 1 )
.collect(Collectors.toSet());
}
public static void main(String[] args)
{
List<Integer> list
= Arrays.asList( 5 , 13 , 4 ,
21 , 13 , 27 ,
2 , 59 , 59 , 34 );
System.out.println(
findDuplicateInStream(list));
}
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 Jan, 2023
Like Article
Save Article