CMD Guide
HomeDSABit Manipulation

easy Missing Number

Missing Number

You are given an array nums containing n distinct numbers drawn from the range [0, n] — that is, n + 1 possible values but only n of them present. Exactly one value in [0, n] is missing; return it. For nums = [3, 0, 1] (n = 3, range 0..3), the missing number is 2.

There are several correct answers here, and choosing between them is the actual interview. The naive one sorts or hashes; the arithmetic one is a one-liner with a landmine; the XOR one is landmine-free. Knowing why XOR is the safe pick — not just that it works — is the differentiator.

Baselines and their costs

The bit insight — XOR the indices against the values

XOR has two properties we will lean on, both provable from its truth table: it is self-cancelling (a ^ a = 0, since each bit XORed with itself is 0) and it has an identity (a ^ 0 = a). It is also commutative and associative, so a long XOR chain can be reordered freely — equal terms will find each other and vanish in pairs.

Now line up two sequences: the indices 0, 1, 2, …, n (that is the full range [0, n]) and the values in nums. XOR all of them together into one accumulator. Every number that is actually present appears twice in that combined pile — once as a member of the index range and once as an element of nums — so it cancels to 0. The one missing number appears only once (it is in the range but not in the array), so it is the lone survivor. XOR literally cancels every matched pair and leaves the unmatched value.

Concretely, we fold the range and the array into a single accumulator: start at 0, XOR in each index i and each nums[i], then XOR in the final index n (there is no nums[n], since the array has only n elements at indices 0..n-1).

Traced example — nums = [3, 0, 1], n = 3

We accumulate acc ^= i ^ nums[i] for i = 0, 1, 2, then acc ^= 3. Bits shown are 2-bit patterns (values 0..3):

stepterm(s) XORed inarithmeticacc (binary)
start000 (0)
i=00 ^ nums[0]=30 ^ 0 ^ 3 = 311 (3)
i=11 ^ nums[1]=03 ^ 1 ^ 0 = 210 (2)
i=22 ^ nums[2]=12 ^ 2 ^ 1 = 101 (1)
final^ n = 31 ^ 3 = 210 (2)

The survivor is 2. Sanity check by grouping the whole XOR pile: indices {0,1,2,3} and values {3,0,1} together are 0,1,2,3,3,0,1. Pair them off: the two 0s cancel, the two 1s cancel, the two 3s cancel, and 2 stands alone. Answer 2, matching the trace.

Java: XOR index against value

public class Solution {
    public int missingNumber(int[] nums) {
        int n = nums.length;
        int acc = n;                    // seed with the top index n (no nums[n] exists)
        for (int i = 0; i < n; i++) {
            acc ^= i ^ nums[i];         // cancel index i against value nums[i]
        }
        return acc;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().missingNumber(new int[]{3, 0, 1}));       // 2
        System.out.println(new Solution().missingNumber(new int[]{0, 1}));          // 2
        System.out.println(new Solution().missingNumber(new int[]{9,6,4,2,3,5,7,0,1})); // 8
    }
}

Go: XOR index against value

package main

import "fmt"

func missingNumber(nums []int) int {
	n := len(nums)
	acc := n // seed with the top index n (no nums[n] exists)
	for i := 0; i < n; i++ {
		acc ^= i ^ nums[i] // cancel index i against value nums[i]
	}
	return acc
}

func main() {
	fmt.Println(missingNumber([]int{3, 0, 1}))              // 2
	fmt.Println(missingNumber([]int{0, 1}))                 // 2
	fmt.Println(missingNumber([]int{9, 6, 4, 2, 3, 5, 7, 0, 1})) // 8
}

Complexity

Same asymptotics as the sum formula and better than sort's O(n log n); same space as the sum formula and better than the hash set's O(n).

Why XOR is preferred when overflow matters

The sum formula and XOR are both O(n)/O(1), so the deciding factor is numerical safety. The sum approach computes n(n+1)/2, a quantity that grows with the input and can exceed Integer.MAX_VALUE (231−1). It overflows around n ≈ 65536 (where n(n+1)/2 passes 231), silently wrapping to a wrong answer unless you widen to long. XOR never grows: a bit of the accumulator is set only if some input had that bit set, so the result's highest set bit can never rise above the highest set bit of the largest input — the accumulator stays inside the inputs' bit-width and cannot overflow. (It may still exceed an individual input in value — 1 ^ 2 = 3 — but never the word width.) That is the crisp reason to prefer it: not that it is cleverer, but that it removes an entire failure mode.

Judgment layer — sum vs. XOR vs. sort vs. hash

ApproachTimeSpaceVerdict
Hash setO(n)O(n)Most readable; the safe default when memory is free and you want obviously-correct code.
Sort + scanO(n log n)O(1)–O(n)Slowest; only pick it if the data must be sorted anyway.
Sum formulaO(n)O(1)Tiny and intuitive, but carries overflow risk — use long or subtract incrementally to stay safe.
XORO(n)O(1)Optimal and overflow-proof; the strongest answer once you can justify the cancellation.

Edge cases & traps

Takeaways

🧩 Pattern · Bit Manipulation

Recognize it: Presence / pairing / parity with XOR and masks → think in bits.

→ more Bit Manipulation problems, grouped
🤖 Don't fully get this? Learn it with Claude

Stuck on Missing Number? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Missing Number** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Missing Number** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Missing Number**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Missing Number**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes