Problem overview
The method missingNumber(nums) receives an array of n distinct numbers, each between 0 and n. The range from 0 to n holds n + 1 numbers, so exactly one is absent. The method returns it.
For [3, 0, 1], n is 3, so the range is 0, 1, 2, 3. The numbers 0, 1 and 3 are present, so the answer is 2. The missing number can be n itself, as in [0, 1] (answer 2), or 0, as in [1] (answer 0).
The statement asks for O(n) time and O(1) extra space.
Approach: let XOR cancel the numbers that are present
A set of the values gives O(n) time but O(n) space, and sorting costs O(n log n). XOR lets you do both in one pass with a single variable.
XOR (^) compares bits: the result bit is 1 where the two bits differ. Three facts matter: x ^ x is 0, x ^ 0 is x, and the order of the operands doesn't matter. So when you XOR a collection of numbers, any number that appears twice cancels out, and what remains is the XOR of the numbers that appear once.
Here nothing repeats, but you can create the pairs yourself. The indices of nums go from 0 to n - 1. Add n and you have every number from 0 to n exactly once. XOR all of those numbers together with all the values in nums. Each value in nums appears once among the indices-plus-n and once among the values, so it cancels. The missing number appears only among the indices-plus-n, so it is what is left.
For [3, 0, 1], result starts at 3 and holds these values after the start and after each index:
[3, 0, 1, 2]
The final value, 2, is the missing number.
Step by step
- Create
resultwithlet, starting atnums.length, which isn. The indices stop atn - 1, songoes in at the start. - Loop over every index
ifrom 0 tonums.length - 1. - XOR both the index and the value into
resultwithresult ^= i ^ nums[i]. - Return
result.
The solution in JavaScript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
missingNumber(nums) {
let result = nums.length;
for (let i = 0; i < nums.length; i++) {
result ^= i ^ nums[i];
}
return result;
}
}
JavaScript details worth knowing:
result ^= i ^ nums[i]meansresult = result ^ (i ^ nums[i]). Because XOR is associative, it makes no difference whether you XOR the index and the value intoresultone after the other or together.- XOR works on 32-bit integers. JavaScript converts both operands to signed 32-bit integers first. The lesson's numbers go up to 100,000, so they fit with room to spare.
- Do not use
+and-by accident with XOR.^is the XOR operator in JavaScript. It is not exponentiation: use**for powers.
Complexity
Time: O(n). The loop runs n times with two XORs per pass.
Space: O(1). Only result and the loop variable are kept.
Edge cases and common mistakes
- The missing number is
n. For[0, 1]the indices are 0 and 1, andnis only inresultfrom the start. Startingresultat 0 would return 0 instead of 2. That is why it starts atnums.length. - The missing number is 0. For
[1]the method returns0. - Smallest input.
[0]hasn = 1and returns1. - Order of the input. It doesn't matter:
[9, 6, 4, 2, 3, 5, 7, 0, 1]returns8. - Sum instead of XOR. Computing
n * (n + 1) / 2minus the sum ofnumsalso works, but the sum of 100,000 numbers is still far below JavaScript's safe integer limit, so it is a matter of taste here. XOR needs no arithmetic at all. - Sorting first. Sorting with no comparator orders numbers as strings:
[10, 9, 1].sort()gives[1, 10, 9]. If you take the sorting route, pass(x, y) => x - y.
Testing
[3, 0, 1]gives2.[0, 1]gives2.[1]gives0.[0]gives1.[2, 0]gives1.[1, 2]gives0.[9, 6, 4, 2, 3, 5, 7, 0, 1]gives8.
