A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/how-to-find-duplicate-elements-in-a-stream-in-java/ below:

How to find duplicate elements in a Stream in Java

How to find duplicate elements in a Stream in Java

Last Updated : 12 Jul, 2025

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

:

  1. 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: Below is the implementation of the above approach: Example: Java
    // 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));
        }
    }
    
  2. 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: Java
    // 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));
        }
    }
    
  3. 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: Java
    // 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