📚 XOR Property for Finding Unique
When every element appears twice except one:
// XOR Property: a ^ a = 0
// All pairs cancel out!
result = 0;
for (num in array) {
result = result ^ num;
}
// result = the unique element
// Example: [4, 1, 2, 1, 2]
// 4 ^ 1 ^ 2 ^ 1 ^ 2
// = 4 ^ (1 ^ 1) ^ (2 ^ 2)
// = 4 ^ 0 ^ 0
// = 4 ✓