C Program to find the element that occurs the most times in an array
Learn how to find the most frequent element in a C array, with defined tie handling, validated code, examples, a dry run, and complexity analysis.
The element that occurs the most times in an array is called the most frequent element or the mode. We can find it by counting the occurrences of each value and keeping the value with the highest count.
For example:
Array: 4 2 4 3 2 4 8
The value 4 occurs three times, more than any other element, so it is the most frequent element.
C Program to Find the Most Frequent Array Element
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
int mostFrequent = array[0];
int highestFrequency = 0;
for (int current = 0; current < size; current++) {
int frequency = 0;
for (int index = 0; index < size; index++) {
if (array[current] == array[index]) {
frequency++;
}
}
if (frequency > highestFrequency) {
highestFrequency = frequency;
mostFrequent = array[current];
}
}
printf("Most frequent element = %d\n", mostFrequent);
printf("Number of occurrences = %d\n", highestFrequency);
return 0;
}
Sample Output
Enter the number of elements: 7
Enter 7 elements:
4 2 4 3 2 4 8
Most frequent element = 4
Number of occurrences = 3
How the Program Works
mostFrequentis initialized with the first array element.- The outer loop selects one value at a time.
frequencyis reset to0for each selected value.- The inner loop compares the selected value with every element and counts its matches.
- If the count is greater than
highestFrequency, both the highest frequency and its value are updated. - After every candidate has been checked, the stored value is the array's mode.
Here is a compact dry run:
| Selected value | Frequency | Highest frequency | Most frequent value |
|---|---|---|---|
| 4 | 3 | 3 | 4 |
| 2 | 2 | 3 | 4 |
| 4 | 3 | 3 | 4 |
| 3 | 1 | 3 | 4 |
| 2 | 2 | 3 | 4 |
| 4 | 3 | 3 | 4 |
| 8 | 1 | 3 | 4 |
Repeated candidates are counted again in this straightforward implementation, but they cannot change the result unless their frequency is greater than the stored maximum.
What Happens When Frequencies Are Tied?
When several values share the highest frequency, this program returns the one that appears earliest in the original array.
Consider:
Array: 4 2 4 2 9
Both 4 and 2 occur twice. The program returns 4 because it appears first. This behavior comes from using > rather than >= in the update condition:
if (frequency > highestFrequency)
An equal frequency does not replace the existing result.
What If Every Element Appears Once?
If all values have a frequency of 1, every element is tied. Following the tie rule, the program returns the first element:
Array: 7 3 9 1
Most frequent element: 7
Number of occurrences: 1
Does It Work with Negative Numbers and Zero?
Yes. The program uses direct equality comparisons, so all int values are supported:
Array: -2 0 -2 5 0 -2
Most frequent element: -2
Number of occurrences: 3
Most Frequent Element Versus Frequency of Every Element
A frequency-reporting program prints a count for every distinct value. This program goes one step further by comparing those counts and returning only the value with the maximum frequency.
Use the frequency-reporting version when all counts are needed. Use this program when only the mode and its count are required.
Alternative Approaches
- Frequency array: Can achieve
O(n)time when values lie in a small, known range. Direct indexing needs special handling for negative numbers and can waste memory for a large range. - Sorting: Equal values become adjacent, allowing runs to be counted in
O(n log n)total time. Sorting changes the original order unless a copy is used, and extra work is needed to preserve the first-occurrence tie rule. - Hash table: Offers expected
O(n)time for arbitrary values, but standard C does not provide a built-in hash-table type.
The nested-loop method is easy to understand, preserves the array, supports every int, and requires no auxiliary data structure.
Time and Space Complexity
- Time complexity:
O(n²)because each of thencandidates is compared with allnarray elements. - Extra space complexity:
O(1)because only counters and result variables are used beyond the input array.
Common Mistakes
- Forgetting to reset
frequencybefore counting the next candidate. - Updating the result for a smaller frequency.
- Using
>=without realizing that it changes the tie behavior to favor a later value. - Assuming that the most frequent value must occur more than once.
- Using array values as frequency-array indices without validating their range.
- Failing to define what should happen when multiple values are tied.
By counting each candidate and retaining the highest count, the program finds the most frequent element while preserving the original array and applying a predictable tie rule.