THIS BLEW MY MIND
Based on The PrimeTime's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
XOR cancels identical values because x XOR x = 0, and it preserves values that occur an odd number of times.
Briefing
XOR’s “memory” and cancellation properties let a missing-number problem be solved without maps or sets—and the same core trick scales to duplicates and even two missing/duplicated values. The key insight is that XORing a multiset of values causes pairs of identical elements to cancel to zero, so only values with odd occurrence counts survive. That turns many seemingly different interview questions into variations of “XOR everything, then use bit-level structure to isolate what’s left.”
The transcript starts by walking through XOR on bit patterns and then generalizes the behavior to integers: XOR is applied bitwise, so it works on more than booleans. From there, several foundational identities drive the method: x XOR 0 = x, x XOR x = 0, and XOR is commutative (order doesn’t matter). The practical consequence is the “duplicate removal” rule: if a value appears twice, XORing the whole collection eliminates it; if it appears once, it remains in the final XOR result.
That rule immediately solves the classic interview task: given an array containing every integer from 1 to n exactly once except one missing value, find the missing number. The approach XORs all array elements together, then XORs that with the XOR of the full range 1..n. Since every present number appears once in the array and once in the full range, they cancel out, leaving exactly the missing integer. The transcript emphasizes that ordering doesn’t matter—XOR’s commutativity makes the method robust.
The same cancellation logic connects to forward error correction (FEC). By XORing multiple packets into a single parity packet, losing one packet can be recovered by XORing the parity with the remaining packets. The missing-number algorithm mirrors this: XORing a “complete set” with a “received set” cancels everything that matches and leaves the missing piece.
Next comes in-place swapping: using XOR, two variables can be swapped without a temporary variable by applying a sequence of XOR assignments that effectively reconstruct each value from the other. After that, the transcript extends the missing-number idea to duplicates. If one number is duplicated instead of missing, XORing the array with the full range leaves the duplicated value (because it appears twice in the array and once in the range, producing an odd-count residue). For two missing numbers (or two duplicated numbers), XOR alone yields only their combined XOR (U XOR V), so the method adds a partition step: find a distinguishing bit (the least significant set bit of U XOR V), split both the full range and the array into two groups based on that bit, and then solve the “single missing” problem inside each partition to recover U and V separately.
The transcript ends by noting the limits: once more than two values are missing or duplicated, the simple XOR partitioning strategy breaks down because multiple combinations can produce the same XOR pattern, requiring more complex techniques beyond XOR-only reasoning.
Cornell Notes
XOR cancels duplicates: x XOR x = 0 and x XOR 0 = x, and XOR is commutative. That means XORing a collection removes any value that appears an even number of times, leaving only values with odd occurrence counts. For the classic “missing number from 1..n,” XOR the array and XOR the full range 1..n; everything cancels except the missing integer. The same cancellation idea underpins forward error correction (FEC), where XOR parity lets recovery of a lost packet. With two missing (or duplicated) values, XOR gives only U XOR V, so a bit-based partition splits the problem into two single-missing subproblems to recover both numbers.
Why does XORing a list “remove” duplicates?
How does XOR find the single missing number in an array containing 1..n except one value?
What does the FEC analogy mean in practical terms?
Why can’t two missing numbers be solved by XOR alone?
How does the partition step recover U and V for two missing numbers?
What breaks when more than two values are missing or duplicated?
Review Questions
- Given a multiset where some values appear three times and others appear once, what does XOR of the whole multiset return and why?
- For the two-missing-numbers method, what property of U XOR V guarantees that a chosen bit will separate U and V into different partitions?
- How would the XOR approach change if the known range were not exactly 1..n but an arbitrary set of potential values?
Key Points
- 1
XOR cancels identical values because x XOR x = 0, and it preserves values that occur an odd number of times.
- 2
The missing-number solution is (XOR of array) XOR (XOR of 1..n), which leaves only the missing integer.
- 3
XOR works bitwise on integers, so the same cancellation logic applies beyond booleans.
- 4
Forward error correction (FEC) uses XOR parity so that losing one packet can be recovered by XORing the parity with the remaining packets.
- 5
Two missing (or duplicated) values require an extra step: compute U XOR V, then partition by a distinguishing bit to reduce to two single-missing subproblems.
- 6
The XOR-only strategy becomes unreliable when more than two values are missing/duplicated because multiple combinations can produce the same XOR result.