Java ProgramsCollectionsFind Duplicate Elements Using HashMap

Find Duplicate Elements Using HashMap in Java

beginner·  Collections  ·  Map

Problem

Tallying every value in a HashMap first, then keeping only the entries whose count is above one, separates 'how often does this appear' from 'which ones repeat'.

Given an array of integers, use a HashMap to find every value that appears more than once.

Input
[2, 7, 4, 7, 9, 2, 6]
Output
Duplicates: 2, 7

Java Program

Java
import java.util.LinkedHashMap; import java.util.Map; public class DuplicateFinderHashMap { public static void main(String[] args) { int[] nums = {2, 7, 4, 7, 9, 2, 6}; Map<Integer, Integer> tally = new LinkedHashMap<>(); for (int num : nums) { tally.put(num, tally.getOrDefault(num, 0) + 1); // read-or-default, then store the incremented count } StringBuilder duplicates = new StringBuilder(); for (Map.Entry<Integer, Integer> entry : tally.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: 2, 7

Core Logic

Building a full tally of every value first, then filtering that tally down to the entries with a count above one, reuses the same map for both counting and detecting.

How It Works
  1. 1A LinkedHashMap<Integer, Integer> named tally records each value's count, in the order values were first seen.
  2. 2tally.getOrDefault(num, 0) reads a value's running count, defaulting to 0 on its first appearance, and put() stores it back incremented.
  3. 3Once the array has been fully tallied, entrySet() is walked once more, checking entry.getValue() > 1 for each value.
  4. 4Every value whose count exceeds one is appended to the result, in the order it was first encountered.
In [2, 7, 4, 7, 9, 2, 6], 4, 9, and 6 each appear once, while 2 and 7 each appear twice — so the values reported are 2, 7.
💡

Key Point: The tally map is doing double duty here — the exact same structure that counts occurrences also answers 'does this repeat', just by checking whether a count is above one.

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

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

Key Concepts

HashMapgetOrDefault()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 DuplicateFinderStream { public static void main(String[] args) { int[] nums = {2, 7, 4, 7, 9, 2, 6}; // Tallies every value, then keeps only the ones that appeared more than once String duplicates = Arrays.stream(nums).boxed() .collect(Collectors.groupingBy(n -> n, LinkedHashMap::new, Collectors.counting())) .entrySet().stream() .filter(e -> e.getValue() > 1) .map(e -> String.valueOf(e.getKey())) .collect(Collectors.joining(", ")); System.out.println("Duplicates: " + duplicates); } }

Output

Duplicates: 2, 7

Core Logic

Grouping the array's values with counting(), then filtering that tally down to counts above one, expresses the same two-step idea — tally, then filter — as a single pipeline.

How It Works
  1. 1Arrays.stream(nums).boxed() turns the int[] into a Stream<Integer>, since groupingBy() needs reference types.
  2. 2Collectors.groupingBy(n -> n, LinkedHashMap::new, Collectors.counting()) tallies every value, keeping first-seen order via the LinkedHashMap factory.
  3. 3.entrySet().stream().filter(e -> e.getValue() > 1) keeps only the entries whose tally exceeds one.
  4. 4.map(Map.Entry::getKey) and Collectors.joining(", ") extract just the repeated keys and join them into the final printed string.
For [2, 7, 4, 7, 9, 2, 6], grouping and filtering finds that only 2 and 7 have a tally above one, joining them into "2, 7".
💡

Key Point: This is the same 'tally, then filter' idea as the primary approach, expressed as a pipeline instead of two explicit loops — the tally map is still built in full before the filtering step runs.

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

Why: groupingBy() visits every element once to build the tally, and the filter/map/join steps afterward each pass over the map's entries, which number at most n.

Key Concepts

StreamCollectors.groupingBy()Collectors.counting()

Related Programs