Two Numbers Appearing Once

Find two unique elements when others appear twice.

Problem: All appear twice except two numbers. Find both.

1.1. XOR all → aba \oplus b

2.2. Find set bit (differs between aa, bb)

3.3. Partition by that bit, XOR each group

xorAll = XOR of all nums
diffBit = xorAll & (-xorAll)
a = b = 0
for num in nums:
    if num & diffBit: a ^= num
    else: b ^= num
return [a, b]

Numbers with same bit cancel in each partition.

Time: O(n)O(n). Space: O(1)O(1).