Missing Number

Implement missingNumber

An array nums contains n distinct numbers, each drawn from the range [0, n] — so exactly one value in that range is missing from the array. Find it. Summing 0 through n and subtracting the array's actual sum gets there directly, though the expected-sum formula can overflow for large n before it's ever halved. XOR sidesteps that entirely: XOR together every number from 0 to n with every value actually in the array, and every number that appears in both cancels itself out — the only one left standing is the missing value.

Example 1:

Input: nums = [3,0,1]

Output: 2

Example 2:

Input: nums = [0,1]

Output: 2

Example 3:

Input: nums = [9,6,4,2,3,5,7,0,1]

Output: 8

+ 7 hidden test cases run on Submit.

Constraints:

  • n == nums.length
  • 1 ≤ n ≤ 10⁵
  • 0 ≤ nums[i] ≤ n
  • All the numbers in nums are unique

nums =

[3, 0, 1]