Missing Number

easy

Find the missing number in array [0, n] using XOR

Missing Number

Key Insight

XOR all indices (0..n) with all array values. Pairs cancel, missing remains.

Step 1Problem
No binary data

Array [3,0,1] is missing one number from [0,1,2,3].

1 / 5

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Missing Number

XOR all indices (0..n) with all array values. Pairs cancel, missing remains.

  1. Problem

    Array [3,0,1] is missing one number from [0,1,2,3].

  2. XOR Indices

    XOR indices: 0 ^ 1 ^ 2 ^ 3 = 0

  3. XOR Values

    XOR array values: 3 ^ 0 ^ 1 = 2

  4. Combine

    (0^1^2^3) ^ (3^0^1) = 2. Pairs cancel!

  5. Result

    Missing number is 2!