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 problem
0
Problem

Single Number

LeetCode #136Easy
XOR fold · pairs cancel

Given a non-empty array where every element appears twice except for one, find that single one using O(1) extra space.

Asked atAmazonGooglePalantir
step 1 / 10
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]

line 1Every value appears exactly twice except one. Obvious approach: count occurrences in a hash map and return the value seen once.