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, length + 1):
        if i not in numbers:
            output.append(i)
    return output
    
listOfNumbers = [1, 2, 3, 5, 6, 7, 8, 9]
print(findMissingNumbers(listOfNumbers))
Output: [4]

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.

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 1261

Leave a Reply