Blind 75
74 / 75Free practice

Missing Number

Easy · Bit manipulation

Jump to code

An array of length n contains distinct integers chosen from 0 through n. Find the one value that is absent.

Examples

Example 1

nums = [3,0,1]

Output: 2

Example 2

nums = [0,1]

Output: 2

Constraints

  • 1 <= nums.length <= 1000
Need a hint?

XOR cancels values that occur twice.

Asked in real interviews

Candidates reported versions of Missing Number from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    3,
    0,
    1
  ]
]
Expected
2
Case 2
Input
[
  [
    0,
    1
  ]
]
Expected
2
Case 3
Input
[
  [
    9,
    6,
    4,
    2,
    3,
    5,
    7,
    0,
    1
  ]
]
Expected
8
Case 4
Input
[
  [
    1,
    2,
    3
  ]
]
Expected
0
Case 5
Input
[
  [
    0,
    1,
    2,
    3
  ]
]
Expected
4

Drafts are saved per language in this browser when storage is available.