Problem
You get an array nums with n different integers. Each integer is in the range from 0 to n. This range has n + 1 numbers, so exactly one number of the range is not in nums.
Return the missing number.
Example 1
- Input:
- nums = [0,4,1,2]
- Output:
- 3
- Explanation:
- The array has 4 items, so the range is 0 to 4. The number 3 is not in the array.
Example 2
- Input:
- nums = [1]
- Output:
- 0
- Explanation:
- The range is 0 to 1, and 0 is not in the array.
Example 3
- Input:
- nums = [0,1,2]
- Output:
- 3
- Explanation:
- The range is 0 to 3. The missing number is 3, which is the largest number of the range.
Constraints
- n == nums.length
- 1 <= n <= 10^4
- 0 <= nums[i] <= n
- All the values of nums are different.
Follow-up
Find a solution that runs in O(n) time and uses O(1) extra space.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 268. Missing Number
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source