Foundation · Module 2 · Lesson 6
XOR patterns
Cancellation, missing values, and unique elements.
Lesson 12 of 123
1d streakXOR Patterns: Self-Cancellation & Linear Missing Value Detection
The exclusive OR (XOR, ^) operator exhibits three fundamental mathematical properties: (1) Identity: x ^ 0 = x; (2) Self-Cancellation: x ^ x = 0; (3) Commutativity & Associativity: a ^ b ^ c = c ^ a ^ b. Because identical numbers cancel out to zero regardless of their order, XOR is the premier tool for finding unique elements and missing numbers in O(N) time with O(1) auxiliary memory.
// Single Number: Every element appears twice except for one
public int singleNumber(int[] nums) {
int unique = 0;
for (int num : nums) {
unique ^= num; // Pairs cancel out to 0; remaining is unique
}
return unique;
}
// Missing Number in range [0, n]
public int missingNumber(int[] nums) {
int xor = nums.length;
for (int i = 0; i < nums.length; i++) {
xor ^= i ^ nums[i]; // Matching indices and values cancel
}
return xor;
}
// Single Number III: Exactly two unique numbers in array of pairs
public int[] singleNumberTwoUniques(int[] nums) {
int xor = 0;
for (int n : nums) xor ^= n; // xor = a ^ b
// Find any bit where a and b differ (e.g. lowest set bit)
int diffBit = xor & (-xor);
int a = 0, b = 0;
for (int n : nums) {
if ((n & diffBit) == 0) a ^= n; // Group 1: bit is 0
else b ^= n; // Group 2: bit is 1
}
return new int[]{a, b};
}Active Recall Checkpoint
In Single Number III (two unique elements a and b), how does partitioning the numbers by xor & (-xor) isolate a and b into separate groups?
Show answer & explanation
Because a != b, xor = a ^ b must contain at least one set bit (where a and b differ). The lowest set bit diffBit is guaranteed to be 1 in one number and 0 in the other. Partitioning the array by this bit places a into one group and b into the other. All other duplicate numbers have identical bits and cancel out within their respective groups.
Use it when
Use XOR cancellation to solve single/missing element problems in O(N) time and O(1) space without allocating hash tables.
Reconsider when
XOR cancellation only works when duplicate counts are even (appearing 2, 4, 6 times). If numbers appear three times, use modulo-3 bit counting instead.
Cost Model
Complexity & Trade-offs
When to use it
- Counting, divisibility, modular cycles, masks, and overflow-safe arithmetic.
- Replacing simulation with a mathematical identity.
- Encoding compact state for search and dynamic programming.
Common mistakes
- Using an identity without checking its domain or overflow behavior.
- Forgetting how the language handles negative remainder and signed shifts.
- Confusing value magnitude with the number of input values.
Practice prompts
- Reproduce the worked XOR patterns example without looking at the trace.
- Change one input condition and explain whether the XOR patterns invariant still holds.
- Implement the approach, derive its cost, and compare it with a straightforward baseline.
Mastery check