Get the item that appears the most times 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
The item that appears most often in an array is usually called the mode. The practical solution is to count how many times each value appears, then keep the one with the largest count. For unsorted input, that gives you a clean linear-time strategy instead of comparing every element with every other element.
Use a Frequency Map
A hash map or dictionary is the standard tool. As you scan the array once, increment the count for each item and track the current best candidate.
This runs in O(n) time on average because each dictionary update is usually constant-time, and it uses O(k) extra space where k is the number of distinct items.
Python Has a Built-In Shortcut
If you are working in Python, collections.Counter is often the clearest high-level answer.
This is concise and readable. Under the hood, it is still doing frequency counting.
Sorting Is Another Option
If the array can be sorted and you do not mind changing order, sorting groups equal values together and lets you find the longest run.
This approach usually costs O(n log n) time because of the sort. It is mainly useful when the data is already sorted or when a frequency map is not convenient.
Ties Need a Rule
The phrase "appears the most" becomes ambiguous when two or more values share the same maximum frequency. You need to decide the expected behavior:
- return the first item to reach the maximum count
- return the smallest or largest value among the tied items
- return every mode
Here is a version that returns all modes.
Without a tie rule, two correct implementations can produce different answers.
Common Pitfalls
A common mistake is reaching for a nested loop first. That works for tiny arrays, but it pushes the runtime to O(n^2) and becomes noticeably worse as the input grows.
Another mistake is ignoring empty input. A function that assumes items[0] exists without checking will crash on an empty array.
Developers also often forget about tie behavior. If the task specification does not define what to do with multiple modes, write that rule down before implementing the function.
Finally, be careful with unhashable values when using a dictionary-based solution. Lists, for example, cannot be used as dictionary keys. In that case you need a different representation or a different counting strategy.
Summary
- The standard efficient solution is to count frequencies with a dictionary or hash map.
- This gives average
O(n)time and extra space proportional to the number of distinct items. - In Python,
Counter.most_common(1)is the shortest high-level approach. - Sorting also works, but it usually costs
O(n log n)time. - Define the tie rule explicitly if more than one value can be the mode.

