Skip to content
AI360Xpert
Beta
Difficulty: EasyBit Manipulation

Missing Number

Problem in Plain English

Find the one number missing from an array that holds n distinct values from the range 0 to n, in linear time with constant extra space.

Problem Statement

Given an array nums holding n distinct numbers drawn from the range 0 to n, return the single number in that range that does not appear.

Sorting exposes the gap but costs more than linear time, and a lookup set costs linear space. The follow-up asks for both at once: linear time with only constant extra space.

Constraints

  • n == nums.length
  • 1≤n≤1041 \le n \le 10^4
  • 0≤nums[i]≤n0 \le \mathrm{nums}[i] \le n
  • All the numbers of nums are unique.

Examples

Example 1

Input
nums = [3, 0, 1]
Output
2

With n = 3 the full range is 0 to 3, and 2 is the only value absent.

Example 2

Input
nums = [0, 1]
Output
2

With n = 2 the full range is 0 to 2, and 2 is the only value absent.

Example 3

Input
nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output
8

With n = 9 the full range is 0 to 9, and 8 is the only value absent.

Example 4

Input
nums = [1]
Output
0

The one-element input must come from [0, 1]. Since 1 is present, the missing value is zero, which must not be confused with a failure sentinel.

Intuition

There are n + 1 possible values but only n distinct array entries, so exactly one value is absent. For [3, 0, 1], compare the full range [0, 1, 2, 3] with the input: only 2 has no partner. A set makes this comparison explicit. XOR compresses it into one accumulator because equal values cancel, regardless of their order in the array.

Approaches

Lookup Set

Solution Details

Reveal Lookup Set: intuition, complexity, and code

Hints

Hint 1
If nothing were missing, the array would hold exactly 0 to n in some order. What single value breaks that picture in [3, 0, 1]?
Hint 2
A set of the values answers every is-it-missing test instantly; what does that storage cost?
Hint 3
XOR each index together with its value into one accumulator seeded with n: pairs cancel and only the missing number survives.

Edge Cases

  • Missing 0, as in [1]: all nonzero values cancel, leaving the valid answer 0.
  • Missing n itself, as in [0, 1]: no index reaches it, but the seed n = 2 survives the folding.
  • Single-element arrays such as [1]: one fold still isolates the missing 0.

Common Mistakes and Interview Tips

  • Sorting first and scanning for the gap, which costs O(nlog⁡n)O(n \log n) time instead of linear.
  • Seeding the accumulator with 0 instead of n, which drops the top of the range and answers 0 whenever n itself is missing.
  • Stopping at the set solution and missing the constant-space follow-up.
  • Interview tip: explain why indices provide the complete expected range only after seeding n. XOR does not detect invalid inputs with duplicates or multiple missing numbers.

Key Takeaway

When a complete range has exactly one hole, pairing each element against its expected partner with XOR exposes the hole with no storage.