Finding the missing number in an array is a popular coding interview question. According to LeetCode, this question is popular in the interviews of companies like Amazon, Adobe, Microsoft, LinkedIn, and many more. If you want to learn how to find the missing number in an array, this article is for you. This article will take you through how to find the missing number using **Python**.

## Find Missing Number using Python

Finding the missing number in an array means finding the numbers missing from the array according to the range of values inside the array. Most of the time, the question you get based on this problem is like:

- Given an array containing a range of numbers from 0 to n with a missing number, find the missing number in the input array.

To find the missing number in an array, we need to iterate over the input array and store the numbers in another array that we didn’t find in the input array while iterating over it. Below is how you can find the missing number in an array or a list using the Python programming language:

def findMissingNumbers(n): numbers = set(n) length = len(n) output = [] for i in range(1, n[-1]): if i not in numbers: output.append(i) return output listOfNumbers = [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 14, 16] print(findMissingNumbers(listOfNumbers))

Output: [4, 12, 15]

So this is how you can find the missing numbers in an array or a list using the Python programming language. You can find many more coding interview questions and Python projects **here**.

### Summary

Finding the missing number in an array is a popular coding interview question. To find the missing number in an array, we need to iterate over the input array and store the numbers in another array that we didn’t find in the input array while iterating over it. I hope you liked this article on how to find the missing number in an array or a list using Python. Feel free to ask valuable questions in the comments section below.

this doesn’t seems to be working for a larger list , [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 13, 14, 16]

Its calculating the list till number 14, as the logic is written only for (length + 1) which is 13 in this case, so (13 + 1 = 14). Hence ignoring number 16, so ideally it should be

for i in range(1, n[-1]) — n[-1] range till the last number.

Thanks for your valuable feedback, I will update the code with your suggestions🤝

what is the use of length = len(n)?

the len function is used here for calculating the number of items

Good work, but this code won’t work if the input array would be out of order.

Here’s the solution:

def findMissingNumbers(n):

numbers = set(n)

x=sorted(numbers)

length = len(n)

output = []

for i in range(0, x[-1]):

if i not in x:

output.append(i)

return output

listOfNumbers = [3, 5, 6, 8, 9, 1]

print(findMissingNumbers(listOfNumbers))

and what if its just within the range of the smallest and largest number of the list? This could be the solution

def findMissingNumber(n):

numbers = set(n)

numbers = sorted(numbers)

output = []

for i in range(n[0], n[-1]):

if i not in numbers:

output.append(i)

return output

num = [12, 25,32,45,32]

print(findMissingNumber(num))

def findMissingNumbers(n):

numbers = set(n)

b = max(numbers)

a = min (numbers)

output = []

for i in range(a, b):

if i not in numbers:

output.append(i)

return output

listOfNumbers = [-4, -1, 1, 2, 3, 5, 6, 7, 8, 22, 10, 18, 13, 14, 16, 20]

print(findMissingNumbers(listOfNumbers))

we do not even need the set I believe, gives the same result:

def find_missing_number_in(a_list):

result_list = []

for num in range(min(a_list), max(a_list)):

if num not in a_list:

result_list.append(num)

return result_list

nice artice, thanks Aman, I’m working my way through it all 🙂