Missing Number LeetCode

Key takeaways:

  • The missing number in an array of distinct numbers from the range can be found using the XOR operation, which cancels out all existing numbers, leaving only the missing one.

  • The algorithm runs in O(n) time and uses O(1) extra space, making it an optimal solution for this problem.

  • This problem has real-world relevance in areas like database index reconstruction and sequence alignment in bioinformatics, where data integrity is critical.

In the field of algorithmic challenges, the missing number problem on LeetCode is a great way to learn more about how to work with lists and find things within them. This kind of problem is especially important in situations where data integrity is important, such as database index reconstruction or sequence alignment in bioinformatics.

Problem statement

Given an array, nums, containing nn distinct numbers in the range [0,n][0, n], return the only number in the range that is missing from the array.

Constraints

  • n=n = nums.length
  • 1n1031 \leq n \leq 10^3
  • 00 \leq nums[i] n\leq n
  • There are no duplicates in the array.

Let’s gain a better understanding of the problem with the following examples:

canvasAnimation-image
1 of 2

Knowledge test

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Missing Number

1

What will be the output if this array is given as input?

[0,2,4,6,7,8,3,9,5,10][0, 2, 4, 6, 7, 8, 3, 9, 5, 10]

A)

11

B)

2

C)

1

D)

3

Question 1 of 30 attempted

Solution

To find the missing number in an array containing nn distinct numbers taken from the range [0,n][0, n], we use an efficient algorithm that uses the properties of the XOR operation. The algorithm follows these key steps:

  1. We start by initializing a variable, missing, to the length of the input array nums. This value represents nn, the upper bound of the range from which numbers are taken, and ensures that all numbers from 00 to nn are included in the XOR operation.

  2. We iterate through nums, and in each iteration, we update the missing variable by applying the XOR operation with the index of the current element and its corresponding value. The result of this operation is saved in missing. The XOR operation is applied to each index and each element, ensuring that every number in the complete range [0,n][0, n] is XORed exactly once.

  3. After iterating through nums, we return missing, which holds the value of the missing number. This is because the XOR operation cancels out all numbers that appear twice (once as an index and once as an array element), leaving only the missing number.

This algorithm works efficiently due to the properties of the XOR operation: any number XORed with itself results in 00, and any number XORed with 00 remains unchanged. By XORing all indexes and all elements, all present numbers cancel out, and the remaining value is the missing number.

Let’s look at the following illustration for a better understanding:

canvasAnimation-image
1 of 7

Code example

Let’s look at the code for the algorithm we just discussed.

def find_missing_number(nums):
# Initialize the missing number with the length of the array
missing = len(nums)
# Iterate over the array
for i in range(len(nums)):
# Update the missing number by XORing the current index,
# the current element and the missing variable
missing ^= i ^ nums[i]
return missing
def main():
inputnumbers = [[4, 0, 3, 1],
[8, 3, 5, 2, 4, 6, 0, 1],
[1, 2, 3, 4, 6, 7, 8, 9, 10, 5],
[0],
[1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23]
]
i = 1
for x in inputnumbers:
print(i, ".\tnums: ", x, sep = "")
print("\n\tMissing number: ", find_missing_number(x), sep = "")
print("-"*100, "\n", sep = "")
i+=1
if __name__ == "__main__":
main()
Missing number LeetCode

Now, let’s look at the time and space complexity of the solution.

Time complexity

The time complexity of this solution isO(n)O(n), since it iterates over the array only nn times, where nn is the number of elements in nums.

Space complexity

The space complexity of this solution is O(1)O(1), since it uses constant amount of additional space.

Accelerate your coding career with The Coding Career Handbook. Master job-winning strategies, career growth tactics, and unconventional advice tailored for early-career software engineers.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is a missing and repeated number in an array?

A missing number is an element that should be in the sequence but isn’t, while a repeated number appears more than once instead of the missing one.


What is the smallest positive missing number?

It is the smallest positive integer that is not present in the given array, typically found using sorting or hashing.


What is the easiest way to find a missing number?

The simplest way is using the sum formula for natural numbers (n(n+1)/2) and subtracting the sum of the array’s elements.



Free Resources

Copyright ©2025 Educative, Inc. All rights reserved