Open In App

How to Find the Mode of Numbers in an Array in C?

Last Updated : 19 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, the mode of array numbers is the element that appears most frequently in the array. To find the mode, we can count the occurrences of each element and identify the one with the highest count. In this article, we will find the mode of numbers in C.

Example:

Input:
myArray = { 1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2 }

Output:
Mode : 2

Finding Mode of Array Elements in C

We can find the mode of array elements using multiple different methods:

  1. Naive Method
  2. By Sorting Array
  3. By using Hash table

1. Naive Method

To find the mode of the array elements, we need to count the frequency of each element and determine the one with the highest frequency. The findMode function iterates through the array, counts occurrences of each element, and keeps track of the element with the maximum count, which is then returned as the mode.

C Program to Find Mode of Array Elements using Naive Method

The below program demonstrates how we can find the mode of numbers in an array in C.

C
// C Program to Find the Mode of Numbers in an Array

#include <stdio.h>

// Function to find the mode of the array
int findMode(int arr[], int size)
{
    int max_Value = 0, max_Count = 0, i, j;

    // Iterating through each element in the array
    for (i = 0; i < size; ++i)
    {
        int cnt = 0;

        // Counting occurrences of the current element
        for (j = 0; j < size; ++j)
        {
            if (arr[j] == arr[i])
                ++cnt;
        }

        // Updating maxCount and maxValue if current element's count is greater
        if (cnt > max_Count)
        {
            max_Count = cnt;
            max_Value = arr[i];
        }
    }
    return max_Value;
}

// Driver Code
int main()
{
    int myArray[] = {1, 2, 3, 4, 5, 2, 3, 2, 2, 4, 2};
    int size = sizeof(myArray) / sizeof(myArray[0]);

    int mode = findMode(myArray, size);
    printf("Mode : %d\n", mode);

    return 0;
}

Output
Mode : 2

Time Complexity: O(n2), where n is the number of elements in the array.
Auxiliary Space: O(1)

2. Finding Mode by Sorting Array

If the array is sorted, then all the equal elements will be adjacent to each other. We use this idea to sort the array and then find the frequency of all the elements easily as they are adjacent to each other.

C Program to Find Mode of Array Elements by Sorting Array

C
// C Program to illustrate how to find the mode of the
// elements in an unsorted array of ints by sorting the
// array
#include <stdio.h>
#include <stdlib.h>

// Function to compare two integers for qsort
int compare(const void* a, const void* b)
{
    return (*(int*)a - *(int*)b);
}

int main()
{
    // Unsorted array of integers
    int a[] = { 3, 1, 2, 5, 3, 5, 5, 6, 7, 5 };
    int n = sizeof(a) / sizeof(a[0]);

    // Sort the array
    qsort(a, n, sizeof(int), compare);

    // Initialize mode variables
    int mode = a[0];
    int curr_count = 1;
    int max_count = 1;

    // Iterate through the sorted array to find the mode
    for (int i = 1; i < n; ++i) {
        if (a[i] == a[i - 1]) {
            // Increment the count if the current element is
            // equal to the previous one
            ++curr_count;
        }
        else {
            // Check if the count of the previous element is
            // greater than the maximum count
            if (curr_count > max_count) {
                max_count = curr_count;
                mode = a[i - 1];
            }
            // Reset the count for the current element
            curr_count = 1;
        }
    }

    // Check the count of the last element
    if (curr_count > max_count) {
        mode = a[n - 1];
    }

    // Display the mode
    printf("Mode is: %d\n", mode);

    return 0;
}

Output
Mode is: 5

Time Complexity: O(nlogn)
Space Complexity: O(n)

3. Finding Mode by Counting Frequency in HashTable

Hash tables (or even direct address tables) offers very efficient insertion and deletion. Following is the algorithm for finding the mode of the elements in an unsorted array using hash table:

Approach

  1. Create an array count of the size = MAX_VALUE, where MAX_VALUE is the maximum present value in the unsorted array and initialize all the elements to 0.
  2. Start iterating the given array and for every element ele at index i, increment the value count[ele].
  3. After all the elements are traversed, find the maximum element from the count array and return the index of that element.

C Program to Find the Mode by Counting the Frequency in the Direct Address Table

C++
#include <stdio.h>
#include <stdlib.h>

// Function to find the maximum value in an array
int findMax(int arr[], int n)
{
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

// Function to find the most frequent element in an array
int findMode(int arr[], int n)
{
    // Find the maximum value in the array
    int maxVal = findMax(arr, n);

    // Create and initialize the count array
    int* count = (int*)calloc(maxVal + 1, sizeof(int));

    // Iterate through the array and count occurrences of
    // each element
    for (int i = 0; i < n; i++) {
        count[arr[i]]++;
    }

    // Find the index of the maximum value in the count
    // array
    int maxCount = 0;
    int mode = -1;
    for (int i = 0; i <= maxVal; i++) {
        if (count[i] > maxCount) {
            maxCount = count[i];
            mode = i;
        }
    }

    // Free the allocated memory for the count array
    free(count);

    // Return the most frequent element
    return mode;
}

int main()
{
    int arr[] = { 1, 3, 2, 3, 4, 2, 3, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);

    int mode = findMode(arr, n);
    printf("The most frequent element is: %d\n", mode);

    return 0;
}

Output
The most frequent element is: 3

Time Complexity: O(n)
Space Complexity: O(max(n)), where max(n) is the maximum value in the given set of number.


Next Article

Similar Reads