Find the Missing Number in a Sequence

Implement findMissingNumber

You're given an array nums containing n distinct numbers, all drawn from the range 0 to n inclusive. Exactly one number from that range never made it into the array — find and return it. Think about what the numbers 0 through n *should* add up to, and compare that against what they actually add up to in the given array.

Example 1:

Input: nums = [0,1,3,4,5]

Output: 2

Example 2:

Input: nums = [1]

Output: 0

Example 3:

Input: nums = [0,1,2,3,4]

Output: 5

+ 6 hidden test cases run on Submit.

Constraints:

  • 1 ≤ n ≤ 10⁵, where n = nums.length
  • nums contains n distinct numbers taken from the range [0, n]
  • Exactly one number in that range is absent from nums

nums =

[0, 1, 3, 4, 5]