Skip to content
CodeShare
All problems

Missing Number

Find the one number missing from a range.

EasyArraysMath

The array nums contains n distinct numbers taken from the range 0 to n, so exactly one number in that range is missing.

Return the missing number.

Examples

Example 1
Input
nums = [3, 0, 1]
Output
2
Why
n = 3, so the range is 0 to 3, and 2 is missing.
Example 2
Input
nums = [0, 1]
Output
2
Example 3
Input
nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output
8

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= n
  • All numbers in nums are distinct.