Signup/Sign In

C Program To Find the maximum repeating element in an array

We are given an unsorted array that contains elements in the range from 0 to n-1 where n is a positive integer. Our task is to find the maximum repeating element in the given array. But before moving forward if you are not familiar with the concept of the array in C, then do check the article on Arrays in C.

Input : Enter the array elements: 2 3 4 2 5 4 2 2 6 7 8

Output: 2

Explanation: Here, an array is declared with elements 2 3 4 2 5 4 2 2 6 7 8. Here, 2 occurs 4 times, 3 occurs 1 time, 4 occurs 2 times, 5 occurs only once, 6 occurs 1 time, 7 occurs 1 time and 8 occurs 1 time. From the above frequencies, it is clear that the maximum occurring element is 2.

Program 1: Find the Maximum Repeating Element in an Array

This is the simplest method to find the most repeating element in an array. Here, we will use two for loops to count the frequency of each element. The first for loop is used to hold an element and the inner for loop is used to count the occurrence of that element in the remaining array. This will keep track of the maximum element counted and at the same time, it will compare it with the count of the current element. After that, it will return the element with the maximum frequency.

Algorithm

  1. Start
  2. Declare the array.
  3. Initialize the array.
  4. Call the function that will return the most occurring element.
  5. Declare two for loops
  6. The first for loop will hold each element.
  7. The second for loop will check for duplicate elements.
  8. If duplicate elements found, increment the count.
  9. If the count of the current element is more than the maximum count, then the maximum element is updated.
  10. The maximum element counted is returned.
  11. End.

Below is the implementation in the C language.

#include<stdio.h>
//Program to count most occuring element
int getMaxRepeatingElement(int array[], int n) 
{
    int i, j, maxElement, count;
     int maxCount = 0;
    /* Frequency of each element is counted and checked.If it's greater than the utmost count element we found till now, then it is updated accordingly  */  
    for(i = 0; i< n; i++)   //For loop to hold each element
    {
        count = 1;
        for(j = i+1; j < n; j++)  //For loop to check for duplicate elements
        {
            if(array[j] == array[i])
            {
                count++;     //Increment  count
                /* If count of current element is more than 
                maxCount, then update maxElement */
                if(count > maxCount)
                {
                    maxElement = array[j];
                }
            }
        }
    }
    return maxElement;
}
//Driver Program
int main()
{
    int n;              //Array Size Declaration
    printf("Enter the number of elements ");
    scanf("%d",&n);    
    int array[n];      //Array Declaration
    printf("Enter the array elements");
    for(int i=0;i<n;i++)   //Initializing Array Elements
    {
        scanf("%d",&array[i]);
    } 
    int maxElement = getMaxRepeatingElement(array, n);    //Function call
    printf("\n Maximum Repeating Element : %d",maxElement);   //Prints the most occuring element 
    return 0;
}


Enter the number of elements 5
Enter the array elements 2 4 5 3 5
Maximum Repeating Element: 5

Program 2: Find the Maximum Repeating Element in an Array

This is the most efficient method to find the number of most repeating elements in the array. The main concept behind using this approach is that if we sort the array, all the duplicate elements will get lined up next to each other. We can now linearly find the frequency of all elements in the array. This approach also ensures that the frequency is calculated only once for each unique element.

Algorithm

  1. Start
  2. Declare the array.
  3. Initialize the array.
  4. Call the function that will return the most occurring element.
  5. Sort the array first.
  6. Traverse the array to count the frequency of each element.
  7. Return the element with the highest frequency.
  8. Print the element.
  9. End.

Below is the implementation in the C language.

#include<stdio.h>
#include<stdlib.h>
//Program to count most occuring element
int findMostFrequentElement(int A[], int n)
{
    for (int i = 0; i < n; i++)    //Sort the array
    {    
        int temp;
        for (int j = i+1; j < n; j++) 
        {     
           if(A[i] > A[j]) 
           {    
               temp = A[i];    
               A[i] = A[j];    
               A[j] = temp;    
           }     
        }     
    }        
    //finnd the most occuring element
    int max_count = 1, res = A[0], count = 1; 
    for (int i = 1; i < n; i++) { 
        if (A[i] ==A[i - 1]) 
            count++; 
        else { 
            if (count > max_count) { 
                max_count = count; 
                res = A[i - 1]; 
            } 
            count = 1; 
        } 
    }   
    // If last element is most frequent 
    if (count > max_count) 
    { 
        max_count = count; 
        res = A[n - 1]; 
    }   
    return res; //return the most repeatinng  element
}
//Driver Program
int main()
{
    int n;              //Array Size Declaration
    printf("Enter the number of elements ");
    scanf("%d",&n);    
    int array[n];      //Array Declaration
    printf("Enter the array elements");
    for(int i=0;i<n;i++)   //Initializing Array Elements
    {
        scanf("%d",&array[i]);
    } 
    int maxElement = findMostFrequentElement(array, n);    //Function call
    printf("\n Maximum Repeating Element : %d",maxElement);   //Prints the most occuring element 
    return 0;
}


Enter the number of elements 5
Enter the array elements 3 4 6 3 3
Maximum Repeating Element : 3