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

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


  • 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()