array
occurrence
frequency
algorithm
data structure

Determine the most common occurrence 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 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.

python
1def most_common(values):
2    counts = {}
3    best_value = None
4    best_count = 0
5
6    for value in values:
7        counts[value] = counts.get(value, 0) + 1
8        if counts[value] > best_count:
9            best_value = value
10            best_count = counts[value]
11
12    return best_value, best_count
13
14
15data = [4, 2, 4, 3, 4, 2, 1]
16print(most_common(data))

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:

python
1from collections import Counter
2
3data = [4, 2, 4, 3, 4, 2, 1]
4counter = Counter(data)
5
6value, count = counter.most_common(1)[0]
7print(value, count)

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:

python
1from collections import Counter
2
3data = [1, 2, 2, 3, 3]
4counter = Counter(data)
5max_count = max(counter.values())
6modes = [value for value, count in counter.items() if count == max_count]
7
8print(modes, max_count)

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.

python
1def most_common_sorted(values):
2    values = sorted(values)
3    best_value = values[0]
4    best_count = 1
5    current_value = values[0]
6    current_count = 1
7
8    for value in values[1:]:
9        if value == current_value:
10            current_count += 1
11        else:
12            if current_count > best_count:
13                best_value = current_value
14                best_count = current_count
15            current_value = value
16            current_count = 1
17
18    if current_count > best_count:
19        best_value = current_value
20        best_count = current_count
21
22    return best_value, best_count

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_common is 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.

Course illustration
Course illustration

All Rights Reserved.