Get AI summaries of any video or article — Sign up free
THIS BLEW MY MIND thumbnail

THIS BLEW MY MIND

The PrimeTime·
5 min read

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.

TL;DR

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?

Because XOR has the identity x XOR x = 0. When the same value appears twice in the XOR chain, those two occurrences cancel to zero, and the remaining XOR result is unchanged. Since XOR is commutative, the order of operations doesn’t matter—duplicates can be mentally paired up and canceled regardless of where they occur in the list.

How does XOR find the single missing number in an array containing 1..n except one value?

Compute (XOR of all array elements) XOR (XOR of all integers from 1 to n). Every number that is present appears once in the array and once in the full range, so each cancels. Only the missing number appears in the full-range XOR but not in the array XOR, so it remains as the final result.

What does the FEC analogy mean in practical terms?

FEC can XOR multiple packets into one parity packet. If one packet is missing, XORing the parity with the remaining packets reconstructs the missing packet because all the received packets cancel out pairwise, leaving the missing one. The missing-number algorithm is the same algebra: “complete set XOR received set” leaves the missing element.

Why can’t two missing numbers be solved by XOR alone?

If the missing values are U and V, XORing the array with the full range yields U XOR V, not U and V individually. Many different pairs can share the same XOR result, so an extra step is needed to separate them.

How does the partition step recover U and V for two missing numbers?

Let X = U XOR V. Choose a bit where U and V differ—commonly the least significant set bit of X. Partition both the full range and the array into two groups based on whether that bit is 0 or 1. Because U and V fall into different groups, each partition becomes a “single missing number” problem, allowing recovery of U from one side and V from the other.

What breaks when more than two values are missing or duplicated?

With three or more missing/duplicated values, XORing collapses many possibilities into the same combined XOR result. The simple “partition by one distinguishing bit” approach no longer guarantees that each partition isolates exactly one unknown, so XOR-only reasoning becomes insufficient and more complex methods are required.

Review Questions

  1. Given a multiset where some values appear three times and others appear once, what does XOR of the whole multiset return and why?
  2. 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?
  3. 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. 1

    XOR cancels identical values because x XOR x = 0, and it preserves values that occur an odd number of times.

  2. 2

    The missing-number solution is (XOR of array) XOR (XOR of 1..n), which leaves only the missing integer.

  3. 3

    XOR works bitwise on integers, so the same cancellation logic applies beyond booleans.

  4. 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. 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. 6

    The XOR-only strategy becomes unreliable when more than two values are missing/duplicated because multiple combinations can produce the same XOR result.

Highlights

XOR turns “find the missing number” into pure cancellation: XORing the array with the full 1..n range leaves exactly the missing value.
The same algebra explains forward error correction: XOR parity plus received packets reconstructs a lost packet.
For two missing values, XOR yields only U XOR V; splitting by a distinguishing bit converts the problem into two independent single-missing recoveries.
XOR can swap two variables in place using a sequence of XOR assignments that reconstruct each value without temporary storage.

Topics

  • XOR Trick
  • Missing Number
  • Forward Error Correction
  • Bit Partitioning
  • Duplicate Detection

Mentioned

  • FEC
  • UDP
  • SDP
  • RTMP