Patterns
BX · Pattern 18
Bitwise XOR
XOR is its own inverse: a ^ a = 0 and a ^ 0 = a. XOR-ing all elements cancels duplicates, leaving only the unique ones.
- Time
- O(n)
- Space
- O(1)
Spot it when
- Find the single number that appears once
- Find two numbers appearing an odd number of times
- Missing number in range