Missing Number

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Missing Number

Approach

Initialize XOR with n (the array length). Then XOR every index 0..n-1 and every array value together. All present numbers pair with their index and cancel out, leaving only the missing number.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

XOR Pattern

Why It Works

XOR-ing indices 0..n with all array values creates matched pairs for every number except the missing one, and paired XOR cancels to zero.

Updated Feb 2026