SwadhyAI Notes

All Java notes

How to Find Duplicates in Array in Java

The Challenge

Identify and display duplicate elements in an array!

Solutions

Method 1: Using HashSet

java
import java.util.*;

int[] numbers = {1, 2, 3, 2, 4, 3, 5, 3};
Set<Integer> seen = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();

for (int num : numbers) {
    if (!seen.add(num)) {
        duplicates.add(num);
    }
}

System.out.println("Duplicates: " + duplicates);
// Output: Duplicates: [2, 3]

Method 2: Using HashMap (with frequency)

java
import java.util.*;

int[] numbers = {1, 2, 3, 2, 4, 3, 5, 3};
Map<Integer, Integer> freq = new HashMap<>();

for (int num : numbers) {
    freq.put(num, freq.getOrDefault(num, 0) + 1);
}

System.out.println("Duplicates with count:");
for (Map.Entry<Integer, Integer> entry : freq.entrySet()) {
    if (entry.getValue() > 1) {
        System.out.println(entry.getKey() + " appears " + entry.getValue() + " times");
    }
}
// Output:
// 2 appears 2 times
// 3 appears 3 times

Did You Know?

The add() method of HashSet returns false if the element already exists - perfect for detecting duplicates!

Pro Tip

Use HashMap when you need to know how many times each duplicate appears, use HashSet for just identifying duplicates!

Real-World Use Cases

  • Data validation: Find duplicate records
  • Quality control: Identify repeated entries
  • Analytics: Detect anomalies in data
›Short versionOpen if you only have a minute.
  • A value is a duplicate the second time it is seen. Later copies are the same fact.
  • One set of seen values and one set of duplicates records each duplicate once.
  • A nested pair of loops is O(n^2) and easy to write. The sets are expected linear time.
  • Printing inside the loop prints a value again on the third, fourth, and later copies.

Find duplicates

Report values that appear more than once, once each.

Working approach

  • Try to add the value to a seen set.
  • If it was already there, add it to a duplicates set.
  • Use LinkedHashSet when the report should follow the order of the first collision.

Walk the sample

  1. Values 1, 2, 2, 3, 1
  2. 1 and 2 are seen
  3. The second 2 is recorded
  4. The second 1 is recorded
  5. The report is 2, then 1

The trap

A nested scan that prints on every later match reports {1, 1, 1} as three hits. The duplicate value is 1, once.

java
static Set<Integer> duplicates(int[] values) {
    Set<Integer> seen = new HashSet<>();
    Set<Integer> duplicates = new LinkedHashSet<>();
    for (int value : values) {
        if (!seen.add(value)) {
            duplicates.add(value);
        }
    }
    return duplicates;
}

For {1, 1, 1}, how many times should 1 appear in the duplicate report?

Once. The third sighting does not create a new duplicate.

A set ignores the later add. A double loop that prints every pair prints 1 more than once. The nested loops are also quadratic, and a linear pass already finds the set.

Expected time O(n), space O(n). A nested search is O(n^2).

Easy to get wrong

  • Printing a duplicate on every occurrence after the first.
  • Using a double loop on a large array when a set records the same answer.