Operate directly on the 1s and 0s that make up integers in memory.
Everything in a computer is bits. By using bitwise operators (AND &, OR |, XOR ^, Shifts << >>), we can perform logic in a single CPU cycle.
XOR (^) is incredibly useful. It returns 1 if bits are different, 0 if they are the same. This means X ^ X = 0 and X ^ 0 = X. If you XOR all numbers in an array where every number appears twice except for one, all pairs cancel out to 0, leaving only the unique number!
def singleNumber(nums):
result = 0
for num in nums:
# Pairs cancel each other out to 0
# 0 ^ Unique = Unique
result ^= num
return result