Python program to find duplicates in an Array

In this tutorial, you will learn how to find duplicates number in an given array.

After finding duplicates we will print the how many time that element has repeated.

In this Program we will first count the occurrence of all the elements which is present in Array. 

And then we will print only those elements which has repeated more than once.

Also Read This :  Java Program to find duplicates in an Array with explanation.

Python Program to Find duplicates in an array

Program 1: Using for Loop

arr, occur = [], [];
n = int(input("please enter the size of array: "))
for x in range(n):
    occur.append(0)
for x in range(n):
    element = int(input(f"please enter the element of array element between 0 to {n-1} :"))
    arr.append(element)
    occur[arr[x]]=occur[arr[x]]+1
for x in range(n):
    if occur[x]>1:
        print(f"{x} is repeated {occur[x]} times") 

Output:

please enter the size of array: 5
please enter the element of array element between 0 to 4 :3
please enter the element of array element between 0 to 4 :4
please enter the element of array element between 0 to 4 :3
please enter the element of array element between 0 to 4 :4
please enter the element of array element between 0 to 4 :2
3 is repeated 2 times
4 is repeated 2 times

Program 2: Using a Set to Track Seen Elements

This method uses a set to track elements that have already been seen as we iterate through the list. When a duplicate is found, it’s added to a result list.

n = int(input("Please enter the size of the array: "))
arr = []
print("Please enter the numbers in the array:")
for i in range(n):
    arr.append(int(input()))

seen = set()
duplicates = []
for num in arr:
    if num in seen:
        duplicates.append(num)
    else:
        seen.add(num)

print("Duplicates:", duplicates)

Output

Please enter the size of the array: 5
Please enter the numbers in the array:
5
3
6
9
3
Duplicates: [3]

Program 3: Using a Dictionary to Count Occurrences

This method uses a dictionary to count how many times each item appears in the list. It then collects items with a count greater than one as duplicates.

n = int(input("Please enter the size of the array: "))
arr = []
print("Please enter the numbers in the array:")
for i in range(n):
    arr.append(int(input()))
    
counts = {}
duplicates = []
for num in arr:
    if num in counts:
        counts[num] += 1
    else:
        counts[num] = 1
for num, count in counts.items():
    if count > 1:
        duplicates.append(num)


print("Duplicates:", duplicates)

Output

Please enter the size of the array: 5
Please enter the numbers in the array:
5
6
5
6
4
Duplicates: [5, 6]

Program 4: Using List Comprehension and count()

This method is straightforward but less efficient for large lists, as it uses the count() method of the list to find duplicates directly in a list comprehension.

n = int(input("Please enter the size of the array: "))
arr = []
print("Please enter the numbers in the array:")
for i in range(n):
    arr.append(int(input()))
    
duplicate_numbers = list({x for x in arr if arr.count(x) > 1})

print("Duplicates:", duplicate_numbers)

Output

Please enter the size of the array: 5
Please enter the numbers in the array:
4
4
1
1
1
Duplicates: [1, 4]