In this blog post, we learn how to write a C program to find the most popular element in an array? So here we will write the program to find the most popular element in an array. We will also see how to display the most frequent element in an array using C programming.
Example,
Input : arr[] = {1, 3, 2, 1, 4, 1}; Output: 1 Explanation: 3 appears three times in the array which is the maximum frequency.
So let’s see the logic to find the most popular element in an array. Suppose arr is an integer array of size N (arr[N] ), the task is to write the C program to find the most popular element in an array.
If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.
Your free trial is waiting
Solution 1: Brute Force
It is the simplest solution to find the most popular element in an array. In which you need to use nested loops. The outer loop picks elements one by one and the inner loop is scanning picks elements’ entire array to find their duplicates.
Here we are also maintaining two important variables ‘count’ and ‘popular’. The count variable store the maximum frequency and the popular store the element which has a maximum frequency.
#include<stdio.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) int getMostPopularElement(int arr[], const int n) { int count = 1, tempCount; int temp = 0,i = 0,j = 0; //Get first element int popular = arr[0]; for (i = 0; i < (n- 1); i++) { temp = arr[i]; tempCount = 0; for (j = 1; j < n; j++) { if (temp == arr[j]) tempCount++; } if (tempCount > count) { popular = temp; count = tempCount; } } return popular; } int main() { int arr[] = {1, 2, 3, 2, 3, 1, 3, 4, 4}; const int N = ARRAY_SIZE(arr); const int popular = getMostPopularElement(arr, N); // print the most occurring numbers printf(" %d \n\n", popular); return 0; }
Output:
Solution 2. Using Sorting
It is an optimized solution to find the maximum frequency of an element. In this solution first, we will sort the given array. Here I am sorting the array using the qsort library function. If you want you can create your own version of the array sorting function.
Now after sorting the array, all duplicate elements will get aligned. We can now linearly find the frequency of all elements in the array. The time complexity of this solution is O(n log n) and space complexity O(1).
#include<stdio.h> #include <stdlib.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) //call back function int compare(const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } //function to get most popular array element int getMostPopularElement(int arr[], const int n) { //sort array elements using qsort inbuilt function qsort( arr,n, sizeof(int), compare); // find the max popularity of element using linear traversal int count = 1, popular = arr[0], tempCount = 1, i= 0; for (i = 1; i < n; i++) { if (arr[i] == arr[i - 1]) { tempCount++; } else { if (tempCount > count) { count = tempCount; popular = arr[i - 1]; } tempCount = 1; } } // If last element is most frequent if (tempCount > count) { count = tempCount; popular = arr[n - 1]; } return popular; } int main() { int arr[] = {1, 2, 3, 2, 3, 1, 3, 4, 4}; const int N = ARRAY_SIZE(arr); const int popular = getMostPopularElement(arr, N); // print the most occurring numbers printf(" %d \n\n", popular); return 0; }
Recommended Articles for you:
- Best gift for programmers.
- Best electronic kits for programmers.
- C program to find odd occurring elements in an array of limited range
- C program to find even occurring elements in an array of limited range
- Find sum of all sub-array of a given array.
- C program to segregate even and odd numbers
- Find an element in array such that sum of left array is equal to sum of right array.
- C Program to find the count of even and odd elements in the array.
- Write C program to find the sum of array elements.
- Find the sum of array elements using recursion
- C Program to reverse the elements of an array
- C Program to find the maximum and minimum element in the array
- Calculate size of an array in without using sizeof in C
- How to create a dynamic array in C?
- How to access 2d array in C?
- Dangling, Void, Null and Wild Pointers
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Memory Layout in C.
- 100 C interview Questions
- File handling in C.