All Topics

Bit Manipulation

Leverage binary operations for elegant O(1) space solutions and XOR tricks

0/40 solved

XOR Tricks

Use XOR properties to find missing/duplicate numbers without extra space

O(n) O(1)

Approach

XOR all elements together. Since duplicates cancel out (a^a=0), the result is the unique element. For two unique numbers, first XOR all to get xor_result, find any set bit to split into two groups, XOR each group separately.

How to Recognize

1

Find the single number in an array where all others appear twice

2

Find missing or duplicate number using O(1) space

3

XOR of a number with itself is 0; XOR with 0 gives the number

4

Phrases like 'appears exactly once', 'single number'

Pro Tips

a ^ a = 0, a ^ 0 = a — these two properties solve most XOR problems

To find two unique numbers: XOR all, use a set bit to divide into two groups

XOR is commutative and associative — order doesn't matter

8

Total

4

Easy

4

Medium

0

Hard

Time

O(n)

Space

O(1)