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]
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…