Missing Number

You are given an array nums of n distinct integers, each taken from the range 0 to n (both ends included). The range has n + 1 numbers and the array has only n, so exactly one number of the range is missing. Return it.

Aim for O(n) time and O(1) extra space.

Constraints

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

Examples

Example 1

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

There are 4 numbers, so the range is 0 to 4. Only 2 is absent.

Example 2

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

The range is 0 to 3, and 3 itself is the missing one.

Example 3

Input: nums = [1]
Output: 0

The range is 0 to 1, so the missing number is 0.

JavaScript you'll use: Bitwise operators Numbers, Math and integer tricks

/**
 * @param {number[]} nums
 * @return {number}
 */
function missingNumber(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.