Determine the most common occurrence in an array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the most common value in an array means finding the element with the highest frequency, also called the mode. The best approach depends on whether you want one mode, all tied modes, low memory usage, or a special case such as a guaranteed majority element.
The Hash-Map Counting Approach
For general data, the most practical solution is to count occurrences in a dictionary or hash map as you scan the array.
This runs in linear time, O(n), with additional memory proportional to the number of distinct values.
It is the default answer for most real-world inputs because it is both fast and easy to understand.
Using Python's Counter
If you are using Python, collections.Counter is even more convenient:
This is still a counting-based solution, just wrapped in a standard-library utility.
What If Several Values Tie
Some arrays have more than one mode. If you need every tied value, compute the maximum frequency and filter for all elements that match it:
That distinction matters because "the most common value" may not be unique.
Sorting as an Alternative
If extra memory is expensive and you are allowed to reorder the array, sorting is another option. After sorting, identical values become adjacent, so you can count runs.
This uses O(n log n) time because of sorting, but it can reduce the need for an auxiliary hash map if in-place sorting is acceptable.
Majority Element Is a Special Case
If the problem guarantees that some element appears more than half the time, there is a more specialized algorithm called Boyer-Moore majority vote. But that solves a different problem. It does not find the mode in the fully general case.
So before choosing a clever algorithm, check whether your input really has a stronger guarantee than "find the most frequent value."
Common Pitfalls
- Assuming the mode is unique when ties may exist.
- Using a majority-element algorithm for a general frequency problem.
- Sorting the array in place without realizing other code depends on the original order.
- Ignoring the memory cost of a frequency map on very large numbers of distinct values.
- Forgetting to define what should happen for an empty array.
Summary
- The most common general solution is to count frequencies in a hash map.
- In Python,
Counter.most_commonis the most convenient version of that idea. - Sorting is a valid alternative when memory matters more than raw time complexity.
- Ties and empty inputs should be handled explicitly.
- Use special algorithms only when the problem guarantees more than a general mode search.
Related reading
- Determine whether integer is between two other integers
- Determine whether or not there exist two elements in Set S whose sum is exactly x - correct solution?
- Determine Whether Two Date Ranges Overlap
- Determining Floating Point Square Root
- Determine whether an array contains a value
- Determining if an unordered vectorT has all unique elements
- Determining if two rays intersect
- Determining the big-O runtimes of these different loops?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.