AlgoMaster Logo

Missing Number

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We have an array of n distinct integers, and these integers come from the range [0, n]. The range has n + 1 numbers but the array holds only n of them, so exactly one number is missing. Our job is to find it.

We know the complete set in advance. If n = 3, the full set is {0, 1, 2, 3}. The array gives us 3 of those 4 numbers, and we need to find the one left out. Because the range is fixed and known, we can compare the array against it directly instead of searching for an arbitrary value, and that opens up arithmetic and bitwise techniques.

Key Constraints:

  • n == nums.length with 1 <= n <= 10^4. The expected sum n * (n + 1) / 2 reaches about 5 * 10^7 at the maximum n, well within a 32-bit signed integer, so a sum-based approach is safe from overflow here.
  • 0 <= nums[i] <= n. Every value lies in the range [0, n], so an auxiliary boolean array of size n + 1 is also an option.
  • All numbers are unique. Each number from [0, n] appears either zero or one times, so there are no duplicates to handle.

Approach 1: Hash Set

Intuition

Put every array element into a hash set, then scan the expected range [0, n] and return the first number that the set does not contain. Membership checks against a hash set are O(1) on average, so the whole scan is linear.

Algorithm

  1. Create a hash set containing all elements of nums.
  2. Loop through every integer from 0 to n (where n is the length of nums).
  3. For each integer, check if it exists in the set.
  4. Return the first integer not found in the set.

Example Walkthrough

1Initial array: nums = [3, 0, 1], n = 3. Build hash set {0, 1, 3}.
0
3
1
0
2
1
1/4

Code

The hash set uses O(n) extra space to store every element. The next approach drops that to O(1) by using the arithmetic structure of the range [0, n] instead of a separate data structure.

Approach 2: Gauss' Formula (Sum)

Intuition

We know the sum of every number in [0, n] without looking at the array. The formula n * (n + 1) / 2 gives the sum of the integers from 0 to n. Compute the actual sum of the array and subtract it from this expected sum, and the difference is the missing number.

The reason is that removing one number from a complete set reduces the total by exactly that number's value. So expectedSum - actualSum recovers the value that was removed.

Algorithm

  1. Compute the expected sum: n * (n + 1) / 2 where n is the length of the array.
  2. Compute the actual sum of all elements in nums.
  3. Return expectedSum - actualSum.

Example Walkthrough

1n=9. Expected sum = 9*10/2 = 45. Start computing actual sum.
0
9
1
6
2
4
3
2
4
3
5
5
6
7
7
0
8
1
1/5

Code

The sum approach is O(n) time and O(1) space, which meets the follow-up requirement. For this problem's bounds the sum cannot overflow a 32-bit integer, but at larger bounds it could, and the next approach avoids addition entirely by computing the answer with XOR.

Approach 3: XOR (Bit Manipulation)

Intuition

XOR satisfies a ^ a = 0 and a ^ 0 = a: XOR-ing a number with itself cancels it, and XOR-ing with zero leaves it unchanged. XOR is also commutative and associative, so the order of operations does not matter.

XOR every index from 0 to n together with every value in the array. Each number that appears in both the index range and the array is XOR-ed twice and cancels to zero. The missing number appears only in the range, so it is XOR-ed once and remains as the final result.

Algorithm

  1. Initialize xor to 0.
  2. XOR xor with each index from 0 to n.
  3. XOR xor with each element of nums.
  4. After all operations, xor holds the missing number. Return it.

Example Walkthrough

1Start: xor=0, n=3. XOR indices [1..n] with array elements.
0
3
1
0
2
1
1/5

Code