DSAHashingFrequencies

Find All Duplicates Visualizer

What is Find All Duplicates

The array has length n and every value is in 1..n. Some values appear once, some exactly twice. Count with a hash map and collect every key whose count becomes 2. Sorting the collected list makes the output stable.

  • Count, then harvest — or append the moment a count hits 2.
  • Set also works: if x is already in the set, it is a duplicate.
  • Why hashing: a nested scan is O(n²) and will TLE.

How this visualizer works

Array 4, 3, 2, 7, 8, 2, 3, 1. 2 and 3 each appear twice — they flash when their count hits 2.

Interactive trace

hash_table.state

Input
Hash table
Current look-up
Match / window
Stored entry

current_action

Load an input, then press Play or Step.

live_trace.py

1def findDuplicates(arr):
2freq, out = {}, []
3for x in arr:
4freq[x] = freq.get(x,0)+1
5if freq[x] == 2:
6out.append(x)
7return sorted(out)

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def findDuplicates(arr):
2freq, out = {}, []
3for x in arr:
4freq[x] = freq.get(x,0)+1
5if freq[x] == 2:
6out.append(x)
7return sorted(out)

complexity.md

OperationTime
Count passO(n)
Sort answerO(d log d)
SpaceO(n)

Python implementation

solution.py

1def findDuplicates(arr):
2 freq, out = {}, []
3 for x in arr:
4 freq[x] = freq.get(x, 0) + 1
5 if freq[x] == 2:
6 out.append(x)
7 return sorted(out)