DSAHashingSet lookup

Contains Duplicate Visualizer

What is Contains Duplicate

Walk the array once. Before inserting a value into a hash set, ask: have I already seen this number? A set answers that in expected O(1). The first repeat is the answer — no nested loops.

  • Hash set: stores unique keys. x in seen is a probe, seen.add(x) is an insert.
  • Why not sort: sorting is O(n log n) and loses the one-pass story. Hashing keeps the original order of discovery.
  • Memory: the set can grow to n distinct values — that is the trade: extra space for linear time.

How this visualizer works

Default input 1, 3, 2, 3, 5. Watch 1, 3, 2 enter the set, then the second 3 hit an existing key and stop. Change the array and replay.

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 containsDuplicate(arr):
2seen = set()
3for x in arr:
4if x in seen:
5return True
6seen.add(x)
7return False

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def containsDuplicate(arr):
2seen = set()
3for x in arr:
4if x in seen:
5return True
6seen.add(x)
7return False

complexity.md

OperationTime
Lookup / insertO(1) expected
Full scanO(n)
SpaceO(n)

Python implementation

solution.py

1def containsDuplicate(arr):
2 seen = set()
3 for x in arr:
4 if x in seen:
5 return True
6 seen.add(x)
7 return False