 # Python Program to Find a Number Repeating and Missing in an Array or List

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

The task is to discover the missing and recurring numbers in the given n-dimensional array or list. The array’s values are in the inclusive range from 1 to n.

Examples:

Example1:

Input:

Given list = [2, 6, 4, 5, 4, 3]

Output:

The element which is repeating in the given list [2, 6, 4, 5, 4, 3] is [ 4 ]
The missing element in the given list [2, 6, 4, 5, 4, 3] is [ 1 ]

Example2:

Input:

Given list =[7, 1, 3, 7, 2, 5, 8, 6]

Output:

The element which is repeating in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 7 ]
The missing element in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 4 ]

## Program to Find a Number Repeating and Missing in an Array or List in Python

Below are the ways to find a number repeating and missing in the given list.

### Method #1: Using Counter() (Hashing , Static Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list as static input and store it in a variable.
• Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqncyValues)
• Traverse in the freqncyValues using For loop.
• Check if the value(frequency) is greater than 1 or not using the If statement.
• If it is true then print it which is the only repeating element in the given list.
• Calculate the sum of all keys in freqncyValues using the sum() and keys() function and store it in a variable sumele.
• Calculate the total number of unique elements in the given list by calculating the length of the freqncyValues say unqelemts.
• Increase the value of unqelemts by 1.
• Apply the sum of n elements formula using  (n*(n+1))/2 where n is unqelemts.
• Subtract the value of sumele from the above sum and store it in another variable.
• This is the missing element in the given list and prints it.
• The Exit of the program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as static input and store it in a variable.
gvnlst = [2, 6, 4, 5, 4, 3]
# Calculate the frequency of all the given list
# elements using the Counter() function which returns
# the element and its frequency as key-value pair and
# store this dictionary in a variable(say freqncyValues)
freqncyValues = Counter(gvnlst)
# Traverse in the freqncyValues using For loop.
for key in freqncyValues:
# Check if the value(frequency) is greater
# than 1 or not using the If statement.
if(freqncyValues[key] > 1):
# If it is true then print it which is the
# only repeating element in the given list.
print('The element which is repeating in the given list',
gvnlst, 'is [', key, ']')
# Calculate the sum of all keys in freqncyValues
# using the sum() and keys() function and store it in a variable sumele.
sumele = sum(freqncyValues.keys())
# Calculate the total number of unique elements
# in the given list by calculating the length of the freqncyValues say unqelemts.
unqelemts = len(freqncyValues)
# Increase the value of unqelemts by 1.
unqelemts = unqelemts+1
# Apply the sum of n elements formula using  (n*(n+1))/2 where n is unqelemts.
result = ((unqelemts*(unqelemts+1))//2)
# Subtract the value of sumele from the above sum and store it in another variable.
result = result-sumele
# This is the missing element in the given list and prints it.
print('The missing element in the given list', gvnlst, 'is [', result, ']')


Output:

The element which is repeating in the given list [2, 6, 4, 5, 4, 3] is [ 4 ]
The missing element in the given list [2, 6, 4, 5, 4, 3] is [ 1 ]

### Method #2: Using Counter() (Hashing , User Input)

Approach:

• Import the Counter() function from collections using the import keyword.
• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Calculate the frequency of all the given list elements using the Counter() function which returns the element and its frequency as key-value pair and store this dictionary in a variable(say freqncyValues)
• Traverse in the freqncyValues using For loop.
• Check if the value(frequency) is greater than 1 or not using the If statement.
• If it is true then print it which is the only repeating element in the given list.
• Calculate the sum of all keys in freqncyValues using the sum() and keys() function and store it in a variable sumele.
• Calculate the total number of unique elements in the given list by calculating the length of the freqncyValues say unqelemts.
• Increase the value of unqelemts by 1.
• Apply the sum of n elements formula using  (n*(n+1))/2 where n is unqelemts.
• Subtract the value of sumele from the above sum and store it in another variable.
• This is the missing element in the given list and prints it.
• The Exit of the program.

Below is the implementation:

# Import the Counter() function from collections using the import keyword.
from collections import Counter
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlst = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
# Calculate the frequency of all the given list
# elements using the Counter() function which returns
# the element and its frequency as key-value pair and
# store this dictionary in a variable(say freqncyValues)
freqncyValues = Counter(gvnlst)
# Traverse in the freqncyValues using For loop.
for key in freqncyValues:
# Check if the value(frequency) is greater
# than 1 or not using the If statement.
if(freqncyValues[key] > 1):
# If it is true then print it which is the
# only repeating element in the given list.
print('The element which is repeating in the given list',
gvnlst, 'is [', key, ']')
# Calculate the sum of all keys in freqncyValues
# using the sum() and keys() function and store it in a variable sumele.
sumele = sum(freqncyValues.keys())
# Calculate the total number of unique elements
# in the given list by calculating the length of the freqncyValues say unqelemts.
unqelemts = len(freqncyValues)
# Increase the value of unqelemts by 1.
unqelemts = unqelemts+1
# Apply the sum of n elements formula using  (n*(n+1))/2 where n is unqelemts.
result = ((unqelemts*(unqelemts+1))//2)
# Subtract the value of sumele from the above sum and store it in another variable.
result = result-sumele
# This is the missing element in the given list and prints it.
print('The missing element in the given list', gvnlst, 'is [', result, ']')


Output:

Enter some random List Elements separated by spaces = 7 1 3 7 2 5 8 6
The element which is repeating in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 7 ]
The missing element in the given list [7, 1, 3, 7, 2, 5, 8, 6] is [ 4 ]

Related Programs: