Find the most common entry in an array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the most common entry in an array means finding the mode: the value that occurs most often. For general-purpose code, the standard solution is to count frequencies with a hash map and return the value with the largest count.
Count frequencies with a hash map
The most direct solution is to walk through the array once and count how many times each value appears. In Python, Counter makes that easy:
This works well for numbers, strings, and most other hashable values. It usually runs in O(n) time and uses extra memory proportional to the number of distinct values.
If you want to write the logic explicitly, the same idea looks like this:
Tie handling is part of the problem
The phrase "most common" sounds simple, but ties matter. In the array [1, 2, 1, 2], both 1 and 2 are equally frequent.
Before writing the function, decide whether you want:
- the first value that reaches the maximum
- the smallest or largest tied value
- all tied values
If you want all modes, you can return a list:
Without a tie rule, two correct implementations may return different answers.
Sorting also works
If you do not want a hash map, another option is to sort the array first and then scan for the longest run of equal values.
This approach is often easier to adapt if you already need sorted data, but it costs O(n log n) time because of the sort.
Do not confuse mode with majority element
A common mistake is reaching for the Boyer-Moore majority vote algorithm. That algorithm solves a narrower problem: finding an element that appears more than half the time, if one exists.
For example, in [1, 2, 2, 3], the mode is 2, but there is no majority element because 2 does not appear more than half the time. So Boyer-Moore is not a general replacement for frequency counting.
Choose the algorithm based on constraints
For most applications, the hash-map solution is best because it is simple and fast. Sorting becomes reasonable if:
- the data is already almost sorted
- memory pressure makes a large frequency table undesirable
- you want ordered output for later processing anyway
The important thing is to define the behavior for empty arrays and ties before you optimize.
Common Pitfalls
- Forgetting to define what should happen when multiple values are equally common.
- Using a majority-element algorithm for a general mode problem.
- Ignoring the empty-array case and letting the code fail unpredictably.
- Sorting first without realizing the time complexity becomes
O(n log n). - Returning one arbitrary answer from tied values without documenting the rule.
Summary
- The most common entry in an array is the mode.
- A frequency map is usually the simplest and fastest general solution.
- Sorting also works, but it typically costs more time.
- Tie handling must be defined explicitly if more than one value is equally common.
- The mode problem is broader than the special majority-element problem.

