Finding Repeating elements in an Array in Python

Finding Repeating elements in an Array in Python

Repeating elements in an Array in Python

In this section we will discuss the program to find the repeating elements in an array in python programming language. We are given with an array and need to print the elements that occurs more than one times in the given input array.

Method Covered :

  • Method 1 : Using Two loops
  • Method 2 : Using hash Map

Method 1 :

In this method we will count the frequency of each elements using two for loops and print those elements which have frequency not equal to one.

  • To check the status of visited elements create a array of size n.
  • Run a loop from index 0 to n and check if (visited[i]==1) then skip that element.
  • Otherwise create a variable count = 1 to keep the count of frequency.
  • Run a loop from index i+1 to n
  • Check if(arr[i]==arr[j]), then increment the count by 1 and set visited[j]=1.
  • After complete iteration of inner for loop and check if count != 1 then print that element.

Time and Space Complexity

  • Time Complexity : O(n2)
  • Space Complexity : O(n)

Method 1 : Code in Python

Run
# Python 3 program to count unique elements
def count(arr, n):

   # Mark all array elements as not visited
   visited = [False for i in range(n)]

   # Traverse through array elements
   # and count frequencies
   for i in range(n):

     # Skip this element if already
     # processed
     if (visited[i] == True):
        continue

     # Count frequency
     count = 1
     for j in range(i + 1, n, 1):
        if (arr[i] == arr[j]):
          visited[j] = True
          count += 1
     if count != 1 :
        print(arr[i]);


# Driver Code
arr = [10, 30, 40, 20, 10, 20, 50, 10]
n = len(arr)
count(arr, n)

Output

10
20

Method 2 :

In this method we will count use dictionary to count the frequency of each elements and then check if that frequency is equal to 1 or not.

  • Declare a dictionary dict().
  • Start iterating over the entire array
  • If element is present in map, then increase the value of frequency by 1.
  • Otherwise, insert that element in map.
  • After complete iteration over array, start traversing map and check if value is not equal to 1,then print the key value.

Time and Space Complexity

  • Time Complexity : O(n)
  • Space Complexity : O(n)
Repeated elements in python

Method 2 : Code in Python

Run
def count(arr, n):

    mp = dict()
    # Traverse through array elements
    # and count frequencies

    for i in range(n):
        if arr[i] in mp.keys():
            mp[arr[i]] += 1
        else:
            mp[arr[i]] = 1

           
    # Traverse through map and print
    # frequencies

    for x in mp:
        if mp[x]!=1 :
          print(x)
    
# Driver Code 
 
arr = [10, 30, 40, 20, 10, 20, 50, 10] 
n = len(arr) 
count(arr, n)

Output

10
20

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

14 comments on “Finding Repeating elements in an Array in Python”


  • Darshan Poojary

    def largepalin(a):
    newarr=[]
    rp=[]
    for i in range(len(a)):
    if a[i] not in newarr:
    newarr.append(a[i])
    elif a[i] not in rp:
    rp.append(a[i])

    print(rp)
    a=[10, 20, 40, 30, 50, 20, 10, 20]
    largepalin(a)


  • SIVAGNANAM

    arr=list(map(int,input().split()))
    arr=sorted(arr)
    rep=[]
    for i in range(1,len(arr)):
    if arr[i]==arr[i-1]:
    rep.append(arr[i])
    print(rep)


  • DuttLuri Lalithasri

    lst = [10, 20, 40, 30, 50, 20, 10, 20]
    out = []
    for i in lst:
    if i not in out:
    out+=[i]
    for i in out:
    c = lst.count(i)
    if c>1:
    print(i)


  • Harsha

    arr = [10, 20, 40, 30, 50, 20, 10, 20]
    kali=[]
    boli=[]
    for i in range(len(arr)):
    #for j in range(i,len(arr)):
    if arr[i] in kali and arr[i] not in boli:
    boli.append(arr[i])
    if arr[i] not in kali:
    kali.append(arr[i])
    print(boli)


  • RADHIKA

    from collections import Counter
    arr=list(map(int,input().split()))
    for i in set(arr):
    if arr.count(i)>=2:
    print(i)


  • Ruturaj

    arr=[10,20,20,10,30,40,50,40]
    arr1=[]
    for i in range(len(arr)):
    if arr[i] not in arr1:
    arr1.append(arr[i])
    elif arr[i] in arr1:
    print(arr[i], end=”, “)


  • Ujjawal

    from collections import Counter
    a=list(map(int,input().split()))
    arr2=[]
    b=Counter(a)
    #print(b)
    for i in b:
    if a.count(i)>=2:
    arr2.append(i)
    print(*arr2)


  • Prashant

    arr=list(map(int,input(“–>”).split()))
    x=list(dict.fromkeys(arr))
    ans=[]
    for i in x:
    res=arr.count(i)
    if res>1:
    ans.append(i)
    print(*ans)


  • Sanjeet

    //Try this in C:
    #include
    int main()
    {
    int arr[100],n,i,j;
    printf(“Enter the length of array: “);
    scanf(“%d”,&n);
    printf(“Enter the value in array: \n”);
    for(i=0;i<n;i++)
    {
    scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
    for(j=i+1;j<n;j++)
    {
    if(arr[i]==arr[j] && arr[i]!=NULL)
    {
    printf("%d ",arr[i]);
    arr[j]=NULL;
    }
    }
    }
    return 0;
    }


  • Arun

    from array import *
    arr=array(“i”,map(int,input(“ENTER ARRAY ELEMENTS “).split()))
    arr=list(arr)
    try:
    for x in range(len(arr)):
    if arr.count(arr[x])>=2:
    print(arr[x])
    arr.remove(arr[x])
    except:
    print()


    • nitesh

      from collections import*
      l=[1,11,11,2,1,21,2,1,4,4,4,5,5]
      p=[]
      s=Counter(l)
      for i in s:
      if s[i]>1:
      if i not in p:
      print(i)


    • Bharath

      arr = [10, 13,13, 17, 11,11, 34, 21]
      for i in range(0,len(arr)):
      for j in range(i+1,len(arr)):
      if arr[i]==arr[j]:
      print(arr[i])