hash_table.state
Input
Hash table
Current look-up
Match / window
Stored entry
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.
x in seen is a probe, seen.add(x) is an insert.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
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Lookup / insert | O(1) expected |
| Full scan | O(n) |
| Space | O(n) |
Python implementation