Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Find Missing Number in a given Array Using Python

2 min read

Problem Definition

Find the missing numbers in a given list or array using Python.

For example in the arr = [1,2,4,5] the integer '3' is the missing number.

There are multiple ways to solve this problem using Python. In this article, we will cover the most straightforward ones.


Step 1: Create an empty array for missing items

Step 2: Loop over the elements within the range of the first and last element of the array

Step 3: Compare the loop variable with the given array if the value is not present append it to the missing array

Note: The array must be sorted for this to work. Use arr.sort() on an unsorted array before feeding it to the program.

Solution 1

arr = [1,2,3,4,5,6,7,9,10]
missing_elements = []
for ele in range(arr[0], arr[-1]+1):
    if ele not in arr:



2. Using List Comprehension

arr = [1,2,3,4,5,7,6,9,10]
missing_elemnts = [item for item in range(arr[0], arr[-1]+1) if item not in arr]



Using list comprehension we encapsulated the above solution in a single line.

3. Using Set()

Set() is a Python unordered mutable datatype that holds only unique values.

arr = [1,2,3,4,5,7,6,9,10]
missing_value = set(range(arr[0], arr[-1]+1)) - set(arr)



Here we created a set object of having values within the range of initial and final values of the provided array then compared it with the provided array to retrieve the missing value.

Instead of subtraction, we can also use the difference() method of the set().

set(range(arr[0], arr[-1]+1)).difference(arr)


Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read