Finding Repeating elements in an Array in Python

Finding Repeating elements in an Array in Python

Repeating element of an array

In this section, we will learn the Python Program to Find the Elements that have Duplicates. In python we have sets which doesn’t allow duplicate elements. 
We will make use this “SET” concept here. Read the algorithm  to get an idea on how code works 

Given an array, print all element whose frequency is one.

Example

  • Input: a[ ]= { 1,2,5,2,6,7,5 }
  • Output: 2,5

There is two number which has more than one frequency. So there is two number which is repeated.

Working:

  • Step 1: Read the array size
  • Step 2: Initialize empty array
  • Step 3: Read array elements and store in an array
  • Step 4: Iterate through set of array (which eliminates duplicates )
  • Step 5: if the element in the set of array has count more than 1 in array then print that element 

Python code:

Method 1:

size=int(input(“ENTER ARRAY SIZE”))
arr=[]
for i in range(size):
    element=int(input())
    arr.append(element)

 

for i in set(arr):
    if arr.count(i)>1:
        print(i,end=” “)

Method 2:

print(“Enter array elements”)
#Syntax for reading space seperated integers
l=list(map(int,input().split()))
rl=[]
for i in l:
    if(l.count(i)>1 and i not in rl):
        rl.append(i)

 

print(“The repeating elements of array are”)
for i in rl:
    print(i,end=” “)
ENTER ARRAY SIZE 7
1
2
5
2
6
7
5
2 5