Last Updated : 12 Jul, 2025
Given a
streamcontaining 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:
Set.add()
// Java program to find the duplicate
// elements in a Stream using Set
import java.util.*;
import java.util.stream.*;
public class GfG {
// Function to find the
// duplicates in a Stream
public static <T> Set<T>
findDuplicateInStream(Stream<T> stream)
{
// Set to store the duplicate elements
Set<T> items = new HashSet<>();
// Return the set of duplicate elements
return stream
// 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
public static void main(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));
}
}
stream.collect( Collectors.groupingBy(Function.identity(), Collectors.counting()));
// Java program to find the duplicate
// elements in a Stream using Collectors.groupingBy()
import java.util.*;
import java.util.stream.*;
import java.util.function.Function;
public class GfG {
// Function to find the
// duplicates in a Stream
public static <T> Set<T>
findDuplicateInStream(Stream<T> stream)
{
// Return the set of duplicate elements
return stream
// 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
public static void main(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));
}
}
Collections.frequency(list, i)
// Java program to find the duplicate
// elements in a Stream
// using Collections.frequency()
import java.util.*;
import java.util.stream.*;
public class GfG {
// Function to find the
// duplicates in a Stream
public static <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
public static void main(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));
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4