Find The Most Frequent Element in an Array

Written by

Ananya Pandey

Finding the most frequently occurring element in an array is a fairly easy array application that can be used for multiple purposes. For instance, the most common marks scored by a class in a subject, or finding the mode of some ungrouped data.

This program is made easier by defining a function but can also be executed without one.

Algorithm

Step 1: Input size from the user.

Step 2: Input elements of the array.

Step 3: Define a function frequent_element taking the array and size as its parameters.

Step 4: Define a for loop to detect the repeated elements.

Step 5: Define another for loop to check if more than 1 element is repeated.

Step 6: Print the result.

Code:

#include<bits/stdc++.h>

using namespace std;

void frequent_element(int arr[], int n) {

int i, j, max_count = 0; cout << "\nMost occurred number: ";

for (i = 0; i < n; i++) { int count = 1; for (j = i + 1; j < n; j++) if (arr[i] == arr[j]) count++; if (count > max_count) max_count = count; }

// this loop checks if there are more than one elements that are repeated

for (i = 0; i < n; i++) { int count = 1; for (j = i + 1; j < n; j++) if (arr[i] == arr[j]) count++;

if (count == max_count)
cout &lt;&lt; arr[i] &lt;&lt; endl;

} }

int main() { int arr[100], n, i; cout << "Enter size of the array: "; cin >> n; cout << "\nEnter elements in the array: \n"; for (i = 0; i < n; i++) { cout << "Enter Element " << i + 1 << ":"; cin >> arr[i]; }

cout << "Original array: ";

for (i = 0; i < n; i++) cout << arr[i] << " ";

frequent_element(arr, n); return 0; }

Output:

Enter size of the array: 8

Enter elements in the array: Enter Element 1: 3 Enter Element 2: 43 Enter Element 3: 2 Enter Element 4: 3 Enter Element 5: 21 Enter Element 6: 3 Enter Element 7: 43 Enter Element 8: 5 Entered Array: 3 43 2 3 21 3 43 5 Most occurred number: 3

Now, using the above-mentioned algorithm we shall create a program to accept ungrouped data and display the mode of the data.

Mode: It is a measure of central tendency used to describe some data, specifically categorical data. Mode refers to the element with the most frequent occurrence.

#include<bits/stdc++.h>

using namespace std;

void mode(int arr[], int n) {

int i, j, max_count = 0; cout << "\nMode of the data: ";

for (i = 0; i < n; i++) { int count = 1; for (j = i + 1; j < n; j++) if (arr[i] == arr[j]) count++; if (count > max_count) max_count = count; }

// this loop checks if there are more than one elements that are repeated

for (i = 0; i < n; i++) { int count = 1; for (j = i + 1; j < n; j++) if (arr[i] == arr[j]) count++;

if (count == max_count)
cout &lt;&lt; arr[i] &lt;&lt; endl;

} }

int main() { int arr[100], n, i; cout << "Enter data size: "; cin >> n; cout << "\nEnter data elements: \n"; for (i = 0; i < n; i++) { cout << "[" << i << "]: "; cin >> arr[i]; }

cout << "Entered Data: ";

for (i = 0; i < n; i++) cout << arr[i] << " ";

mode(arr, n); return 0; }

Output

Enter data size: 8
Enter data elements:
[0]: 45
[1]: 32
[2]: 34
[3]: 26
[4]: 55
[5]: 41
[6]: 55
[7]: 27
Entered Data: 45 32 34 26 55 41 55 27
Mode of the data: 55

To sum up, we found the most occurring element in an array using one of the array’s primary operations- array searching and using the algorithm provided, devised a code to find the mean of ungrouped data.