Missing Number

Easy

nums contains n distinct numbers taken from the range [0, n]. Return the only number in that range that is missing.

Example 1

missingNumber()
Input
nums = [3,0,1]
Output
2

Why

The range is 0…3 and 2 is missing.

Example 2

missingNumber()
Input
nums = [0,1]
Output
2
Constraints
n == nums.length
1 <= n <= 10^4
All numbers are unique.

Topics

ArraysMath

Implement missingNumber in the editor and return the answer — printing it is never graded.