array
most common entry
algorithm
data analysis
programming

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:

python
1from collections import Counter
2
3values = [4, 2, 4, 7, 4, 2]
4counts = Counter(values)
5mode, frequency = counts.most_common(1)[0]
6
7print(mode)       # 4
8print(frequency)  # 3

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:

python
1def most_common(values):
2    counts = {}
3    for value in values:
4        counts[value] = counts.get(value, 0) + 1
5
6    return max(counts, key=counts.get)
7
8
9print(most_common([4, 2, 4, 7, 4, 2]))

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:

python
1from collections import Counter
2
3def all_modes(values):
4    counts = Counter(values)
5    max_count = max(counts.values())
6    return [value for value, count in counts.items() if count == max_count]
7
8
9print(all_modes([1, 2, 1, 2, 3]))

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.

python
1def mode_by_sorting(values):
2    if not values:
3        raise ValueError("values must not be empty")
4
5    values = sorted(values)
6    best_value = values[0]
7    best_count = 1
8    current_count = 1
9
10    for i in range(1, len(values)):
11        if values[i] == values[i - 1]:
12            current_count += 1
13        else:
14            if current_count > best_count:
15                best_value = values[i - 1]
16                best_count = current_count
17            current_count = 1
18
19    if current_count > best_count:
20        best_value = values[-1]
21
22    return best_value

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.

Course illustration
Course illustration

All Rights Reserved.