How to Find the Mode of Numbers in an Array in C?
Last Updated :
19 Jun, 2024
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:
- Naive Method
- By Sorting Array
- 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;
}
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;
}
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
- 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.
- Start iterating the given array and for every element ele at index i, increment the value count[ele].
- 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;
}
OutputThe 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.
Similar Reads
How to Find the Mode of Numbers in a Sorted Array in C?
The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will learn how to find the mode of all elements in a sorted array of integers in C. Example: Input:myArray = {1, 2, 3, 3, 5, 5, 5, 5, 6, 7}
2 min read
How to Find the Range of Numbers in an Array in C?
The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra
2 min read
How to Find Median of Numbers in an Array in C?
For an odd number of elements in an array, the median is the middle element, and for an even number, itâs the average of the two middle elements. In this article, we will learn how to find median of numbers in an array in C. The median of an array can be determined is by first sorting the array and
3 min read
How to Find the Size of an Array in C?
The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C. The simplest method to find the size of an array in C is by using sizeof operator. First determine
2 min read
How to Find Maximum Value in an Array in C?
In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C. Example Input: arr = {5,3,1,2,4} Output: The maximum value of the array is: 5Finding Maximum Value in an Array in CW
2 min read
How to Find Size of Dynamic Array in C?
In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. Itâs particularly useful when dealing with arrays where the size isnât known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size o
2 min read
C Program to Sort an Array in Ascending Order
Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element. The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:
3 min read
How to Use bsearch with an Array in C?
The bsearch() is a library function in C that is used to perform binary search on a given sorted array. In this article, we will learn how to use search on an array in C. Example Input: arr[]= { 2, 5, 7, 11, 15 };Key=15Output:Value 15 found!bsearch() with an Array in CTo use bsearch() on a given arr
2 min read
C program to count Positive and Negative numbers in an Array
Given an array arr of integers of size N, the task is to find the count of positive numbers and negative numbers in the array Examples: Input: arr[] = {2, -1, 5, 6, 0, -3} Output: Positive elements = 3 Negative elements = 2 There are 3 positive, 2 negative, and 1 zero. Input: arr[] = {4, 0, -2, -9,
2 min read
How to Initialize Array of Pointers in C?
Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe
2 min read