0
0
0
0
[0][1][2][3]
Brute · hash count
▸1count ← {}2for x in nums: count[x]++3return the key with count == 1
state
- nums[4, 1, 2, 1, 2]
Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.
Try one problemGiven a non-empty array where every element appears twice except for one, find that single one using O(1) extra space.
▸1count ← {}2for x in nums: count[x]++3return the key with count == 1
line 1Every value appears exactly twice except one. Obvious approach: count occurrences in a hash map and return the value seen once.