TutorialStudyMite

Find The Most Frequent Element in an Array

AAnanya Pandey3 min read
Beginner friendly

Track completion, mastery, and revision.

Finding the most frequently occurring element in an array is a common array operation with multiple applications. For instance, it can be used to determine:

  • The most common marks scored by a class in a subject.

  • The mode of ungrouped data.

This program can be implemented either with or without functions, but using functions improves modularity.

Algorithm

1. Input the array size from the user.

2. Input the array elements.

3. Define a function frequent_element that takes the array and its size as parameters.

4. Use nested loops to:

   - Count occurrences of each element.

   - Track the maximum occurrence count.

5. Check if multiple elements have the same maximum frequency.

6. Print the result(s).

Code


#include <bits/stdc++.h>

using namespace std;

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

    int max_count = 0;

    // Find the maximum occurrence count

    for (int i = 0; i < n; i++) {

        int count = 1;

        for (int j = i + 1; j < n; j++) {

            if (arr[i] == arr[j]) count++;

        }

        if (count > max_count) max_count = count;

    }

    // Print all elements with max_count occurrences

    cout << "\nMost frequent number(s): ";

    for (int i = 0; i < n; i++) {

        int count = 1;

        for (int j = i + 1; j < n; j++) {

            if (arr[i] == arr[j]) count++;

        }

        if (count == max_count) {

            cout << arr[i] << " ";

        }

    }

    cout << endl;

}

int main() {

    int arr[100], n;

    cout << "Enter array size: ";

    cin >> n;

    cout << "Enter array elements:\n";

    for (int i = 0; i < n; i++) {

        cout << "Element " << i + 1 << ": ";

        cin >> arr[i];

    }

    cout << "Original array: ";

    for (int i = 0; i < n; i++) {

        cout << arr[i] << " ";

    }

    frequent_element(arr, n);

    return 0;

}

Output


Enter array size: 8

Enter array elements:

Element 1: 3

Element 2: 43

Element 3: 2

Element 4: 3

Element 5: 21

Element 6: 3

Element 7: 43

Element 8: 5

Original array: 3 43 2 3 21 3 43 5

Most frequent number(s): 3


Finding the Mode of Ungrouped Data

Mode is a measure of central tendency that identifies the most frequently occurring value in a dataset, particularly useful for categorical data.

Code


#include <bits/stdc++.h>

using namespace std;

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

    int max_count = 0;

    // Find maximum frequency

    for (int i = 0; i < n; i++) {

        int count = 1;

        for (int j = i + 1; j < n; j++) {

            if (arr[i] == arr[j]) count++;

        }

        if (count > max_count) max_count = count;

    }

    // Print all modes

    cout << "\nMode(s): ";

    for (int i = 0; i < n; i++) {

        int count = 1;

        for (int j = i + 1; j < n; j++) {

            if (arr[i] == arr[j]) count++;

        }

        if (count == max_count) {

            cout << arr[i] << " ";

        }

    }

    cout << endl;

}

int main() {

    int arr[100], n;

    cout << "Enter data size: ";

    cin >> n;

    cout << "Enter data elements:\n";

    for (int i = 0; i < n; i++) {

        cout << "[" << i << "]: ";

        cin >> arr[i];

    }

    cout << "Entered data: ";

    for (int i = 0; i < n; i++) {

        cout << arr[i] << " ";

    }

    find_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(s): 55

Summary

We demonstrated how to find the most frequent element in an array using basic search operations. The same logic was extended to calculate the mode of ungrouped data. This approach efficiently handles categorical data analysis.

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.