Java ProgramsArraysFind Duplicate Elements

Find Duplicate Elements in Java

intermediate·  Arrays  ·  Array

Problem

A duplicate element is one that appears two or more times in an array, as opposed to an element that appears exactly once.

Given an array of integers, find every element that appears more than once.

Input
[5, 3, 8, 3, 9, 5, 1]
Output
Duplicates: 5, 3

Java Program

Java
import java.util.LinkedHashMap; import java.util.Map; public class FindDuplicateElements { public static void main(String[] args) { int[] arr = {5, 3, 8, 3, 9, 5, 1}; Map<Integer, Integer> freq = new LinkedHashMap<>(); for (int num : arr) { // getOrDefault(num, 0) reads the current count, or 0 if unseen, then increments it freq.put(num, freq.getOrDefault(num, 0) + 1); } StringBuilder duplicates = new StringBuilder(); for (Map.Entry<Integer, Integer> entry : freq.entrySet()) { if (entry.getValue() > 1) { // appeared more than once if (duplicates.length() > 0) duplicates.append(", "); duplicates.append(entry.getKey()); } } System.out.println("Duplicates: " + duplicates); } }

Output

Duplicates: 5, 3

Core Logic

Building a frequency map first, then keeping only the entries with a count greater than one, separates 'how often' from 'which ones repeat'.

How It Works
  1. 1A LinkedHashMap<Integer, Integer> named freq tracks each element's count, preserving the order elements were first seen.
  2. 2freq.getOrDefault(num, 0) reads the current count, or 0 if unseen, then freq.put(num, ...) stores the incremented count.
  3. 3After the full scan, entrySet() is walked once more, checking entry.getValue() > 1 for each element.
  4. 4Every element whose count exceeds one is appended to the result, in the order it was first seen.
In [5, 3, 8, 3, 9, 5, 1], 8, 9, and 1 each appear once, while 5 and 3 each appear twice — so the duplicates reported are 5, 3.
💡

Key Point: This is the same frequency map used to count how many times each element appears — finding duplicates is just a filter applied on top of it.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Building the frequency map visits every element once and holds one entry per distinct value, up to n in the worst case.

Key Concepts

LinkedHashMapgetOrDefault()entrySet()

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; public class FindDuplicateElementsStream { public static void main(String[] args) { int[] arr = {5, 3, 8, 3, 9, 5, 1}; Map<Integer, Long> freq = Arrays.stream(arr).boxed() .collect(Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting())); // Keeps only the elements whose count is greater than one String duplicates = freq.entrySet().stream() .filter(e -> e.getValue() > 1) .map(Map.Entry::getKey) .map(String::valueOf) .collect(Collectors.joining(", ")); System.out.println("Duplicates: " + duplicates); } }

Output

Duplicates: 5, 3

Core Logic

groupingBy plus counting() builds the same frequency map declaratively, and a stream filter keeps only the entries with a count above one.

How It Works
  1. 1Arrays.stream(arr).boxed() turns the int[] into a Stream<Integer>.
  2. 2Collectors.groupingBy(num -> num, LinkedHashMap::new, Collectors.counting()) groups equal elements and counts each group, in first-appearance order.
  3. 3freq.entrySet().stream().filter(e -> e.getValue() > 1) keeps only the entries whose count is greater than one.
  4. 4.map(Map.Entry::getKey) pulls just the element out of each surviving entry, and Collectors.joining(", ") joins them into the final result.
Grouping [5, 3, 8, 3, 9, 5, 1] and filtering for counts above one keeps 5 and 3, the same duplicates the manual version found.
💡

Key Point: The filter condition — count > 1 — is the exact same rule the manual version's second loop checks, just expressed as a stream predicate instead of an if statement.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: groupingBy() still visits every element once while building the map, which holds one entry per distinct value just like the manual version.

Key Concepts

StreamCollectors.groupingBy()Collectors.counting()

Related Programs