Missing Number

Medium~15 min

Given an array nums containing n distinct numbers in the range [0, n], return the one number in the range that is missing from the array.

For example, if nums = [3, 0, 1], the array has 3 elements so the range is [0, 3]. The number 2 is missing.

Examples

Example 1
Input: nums = [3, 0, 1]
Output: 2
Explanation: The range is [0, 3]. The numbers 0, 1, and 3 are present, so 2 is missing.
Example 2
Input: nums = [0, 1]
Output: 2
Explanation: The range is [0, 2]. The numbers 0 and 1 are present, so 2 is missing.
Example 3
Input: nums = [9, 6, 4, 2, 3, 5, 7, 0, 1]
Output: 8
Explanation: The range is [0, 9]. All numbers except 8 are present.

Constraints

  • n == nums.length
  • 1 <= n <= 10^4
  • 0 <= nums[i] <= n
  • All values of nums are unique
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run