268. Missing Number
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find a single missing number from an array nums. The array contains n distinct numbers, and these numbers are taken from the range [0, n]. This means the complete set of numbers should be 0, 1, 2, ..., n, which has n+1 elements. Since nums only has n elements, exactly one number is missing. Our goal is to find this missing number efficiently, specifically with O(n) time complexity and O(1) extra space complexity.
Solution
Missing Number in Python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
missing = n
# The logic is based on the property of XOR. If we XOR all numbers
# from the expected range [0, n] and all numbers from the given array,
# the duplicates will cancel each other out (since x ^ x = 0),
# leaving only the missing number.
# We can achieve this efficiently in one loop.
# Initialize `missing` to `n`. `n` is in the range [0, n] but there is no index `n`.
# Then, loop from i = 0 to n-1. In each step, we XOR `missing` with the index `i`
# and the number `nums[i]`.
# The final result is: n ^ (0^nums[0]) ^ (1^nums[1]) ^ ... ^ ((n-1)^nums[n-1])
# This is equivalent to: (0^1^...^n) ^ (nums[0]^nums[1]^...^nums[n-1])
for i in range(n):
missing ^= i ^ nums[i]
return missing
Missing Number in Java
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int missing = n;
// The logic is based on the property of XOR. If we XOR all numbers
// from the expected range [0, n] and all numbers from the given array,
// the duplicates will cancel each other out (since x ^ x = 0),
// leaving only the missing number.
// We can achieve this efficiently in one loop.
// Initialize `missing` to `n`. `n` is in the range [0, n] but there is no index `n`.
// Then, loop from i = 0 to n-1. In each step, we XOR `missing` with the index `i`
// and the number `nums[i]`.
// The final result is: n ^ (0^nums[0]) ^ (1^nums[1]) ^ ... ^ (n-1^nums[n-1])
// This is equivalent to: (0^1^...^n) ^ (nums[0]^nums[1]^...^nums[n-1])
for (int i = 0; i < n; i++) {
missing ^= i ^ nums[i];
}
return missing;
}
}
Missing Number in C++
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int missing = n;
// The logic is based on the property of XOR. If we XOR all numbers
// from the expected range [0, n] and all numbers from the given array,
// the duplicates will cancel each other out (since x ^ x = 0),
// leaving only the missing number.
// We can achieve this efficiently in one loop.
// Initialize `missing` to `n`. `n` is in the range [0, n] but there is no index `n`.
// Then, loop from i = 0 to n-1. In each step, we XOR `missing` with the index `i`
// and the number `nums[i]`.
// The final result is: n ^ (0^nums[0]) ^ (1^nums[1]) ^ ... ^ (n-1^nums[n-1])
// This is equivalent to: (0^1^...^n) ^ (nums[0]^nums[1]^...^nums[n-1])
for (int i = 0; i < n; ++i) {
missing ^= i; // XOR with the index
missing ^= nums[i]; // XOR with the number at that index
}
return missing;
}
};
Missing Number in TypeScript
function missingNumber(nums: number[]): number {
const n = nums.length;
let missing = n;
// The logic is based on the property of XOR. If we XOR all numbers
// from the expected range [0, n] and all numbers from the given array,
// the duplicates will cancel each other out (since x ^ x = 0),
// leaving only the missing number.
// We can achieve this efficiently in one loop.
// Initialize `missing` to `n`. `n` is in the range [0, n] but there is no index `n`.
// Then, loop from i = 0 to n-1. In each step, we XOR `missing` with the index `i`
// and the number `nums[i]`.
// The final result is: n ^ (0^nums[0]) ^ (1^nums[1]) ^ ... ^ (n-1^nums[n-1])
// This is equivalent to: (0^1^...^n) ^ (nums[0]^nums[1]^...^nums[n-1])
for (let i = 0; i < n; i++) {
missing ^= i ^ nums[i];
}
return missing;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.