Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Finding repeating elements in an array using Python

Repeating elements in an array using Python

Repeating elements are the elements which appear more than once in an array. In this program we will find such elements in an array using python. Initially we will find the occurrence of each element, if the occurrence is more than once then the element is termed as repeating element.

  • Example : Consider the array elements 3,7,9,3,5,2,1,3,5
    • The elements with more than one occurrence are 3,5
    • The repeating elements are 3,5
Finding Repeating elements in an Array in Python

Algorithm

  • Input the elements of an array
  • Iterate through the elements of an array and find the count of each element.
  • If count of an element is greater than 1,then append them to a new array
  • The new array is nothing but repeating element array.
  • Display new array

Python Code:

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=” “)


 

Output:

Enter array elements
3 4 3 5 6 2 1 2
The repeating elements of array are
3 2