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.
Algorithm
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:
missing_elements.append[ele]
print[missing_elements]
Output:
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]
print[missing_elemnts]
Output:
[8]
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]
print[missing_value]
Output:
{8}
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]
PROGRAMS
Latest Articles
Latest from djangocentral
Django 4.1 adds async-compatible interface to QuerySet
The much-awaited pull request for an async-compatible interface to Queryset just got merged into the main branch of Django.Pull Request - //github.com/django/django/pull/14843 The Django core team has been progressively adding async suppor…
Making Django Admin Jazzy With django-jazzmin
Django admin is undoubtedly one of the most useful apps of Django. Over the years there has been very little change in the admin app as far as the UX is concerned and it's not a bad thing at all. Django admin was designed to provide a simple and minimali…
© —
djangocentral | Djangocentral is not associated with the DSF | Django is a registered trademark of the Django Software Foundation
Given an array arr[] of size N-1 with integers in the range of [1, N], the task is to find the missing number from the first N integers.
Note: There are no duplicates in the list.
Examples:
Input: arr[] = {1, 2, 4, 6, 3, 7, 8}, N = 8
Output: 5
Explanation: The missing number between 1 to 8 is 5Input: arr[] = {1, 2, 3, 5}, N = 5
Output: 4
Explanation: The missing number between 1 to 5 is 4
Approach 1 [Using Hashing]:The idea behind the following approach is
The numbers will be in the range [1, N], an array of size N can be maintained to keep record of the elements present in the given array
- Create a temp array temp[] of size n + 1 with all initial values as 0.
- Traverse the input array arr[], and do following for each arr[i]
- if[temp[arr[i]] == 0] temp[arr[i]] = 1
- Traverse temp[] and output the array element having value as 0 [This is the missing element].
Below is the implementation of the above approach:
C++
#include
using
namespace
std;
void
findMissing[
int
arr[],
int
N]
{
int
i;
int
temp[N + 1];
for
[
int
i = 0; i
Javascript
function
getMissingNo[a, n] {
let total = Math.floor[[n + 1] * [n + 2] / 2];
for
[let i = 0; i < n; i++]
total -= a[i];
return
total;
}
let arr = [ 1, 2, 3, 5 ];
let N = arr.length;
let miss = getMissingNo[arr, N];
document.write[miss];
Time Complexity: O[N]
Auxiliary Space: O[1]
Modification for Overflow: The approach remains the same but there can be an overflow if N is large.
In order to avoid integer overflow, pick one number from the range [1, N] and subtract a number from the given array [don’t subtract the same number twice]. This way there won’t be any integer overflow.
Algorithm:
- Create a variable sum = 1 which will store the missing number and a counter variable c = 2.
- Traverse the array from start to end.
- Update the value of sum as sum = sum – array[i] + c and increment c by 1. This performs the task mentioned in the above idea]
- Print the missing number as a sum.
Below is the implementation of the above approach:
C++
#include
using
namespace
std;
int
getMissingNo[
int
a[],
int
n]
{
int
i, total = 1;
for
[i = 2; i
Javascript
function
getMissingNo[a, n]
{
var
x1 = a[0];
var
x2 = 1;
for
[
var
i = 1; i < n; i++] x1 = x1 ^ a[i];
for
[
var
i = 2; i
0
]:
ans
=
i
+
1
print
[ans]
if
__name__
=
=
'__main__'
:
arr
=
[
1
,
3
,
7
,
5
,
6
,
2
]
n
=
len
[arr]
findMissing[arr, n]
Javascript
function
findMissing[arr,size]{
let i;
for
[i = 0; i < size; i++] {
if
[Math.abs[arr[i]] - 1 == size] {
continue
;
}
let ind = Math.abs[arr[i]] - 1;
arr[ind] *= -1;
}
let ans = size + 1;
for
[i = 0; i < size; i++] {
if
[arr[i] > 0]
ans = i + 1;
}
console.log[ans];
}
let arr = [ 1, 3, 7, 5, 6, 2 ];
let n = arr.length;
findMissing[arr,n];
Time Complexity: O[N]
Auxiliary Space: O[1]