Find Missing Number using Python

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.

Aman Kharwal
Aman Kharwal

Data Strategist at Statso. My aim is to decode data science for the real world in the most simple words.

Articles: 1610

8 Comments

  1. 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.

  2. 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))

  3. 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))

  4. 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))

  5. 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 🙂

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading