Array
Frequency Count
Programming
Algorithms
Data Structures

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.

python
1def most_frequent(items):
2    if not items:
3        return None
4
5    counts = {}
6    best_item = items[0]
7    best_count = 0
8
9    for item in items:
10        counts[item] = counts.get(item, 0) + 1
11
12        if counts[item] > best_count:
13            best_item = item
14            best_count = counts[item]
15
16    return best_item
17
18print(most_frequent([3, 1, 3, 2, 3, 2, 1]))

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.

python
1from collections import Counter
2
3items = ["apple", "banana", "apple", "pear", "apple", "banana"]
4counts = Counter(items)
5mode, frequency = counts.most_common(1)[0]
6
7print(mode)
8print(frequency)

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.

python
1def most_frequent_sorted(items):
2    if not items:
3        return None
4
5    items = sorted(items)
6    best_item = items[0]
7    best_count = 1
8    current_item = items[0]
9    current_count = 1
10
11    for item in items[1:]:
12        if item == current_item:
13            current_count += 1
14        else:
15            if current_count > best_count:
16                best_item = current_item
17                best_count = current_count
18            current_item = item
19            current_count = 1
20
21    if current_count > best_count:
22        best_item = current_item
23
24    return best_item

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.

python
1def all_modes(items):
2    counts = {}
3    for item in items:
4        counts[item] = counts.get(item, 0) + 1
5
6    if not counts:
7        return []
8
9    max_count = max(counts.values())
10    return [item for item, count in counts.items() if count == max_count]
11
12print(all_modes([1, 2, 2, 3, 3]))

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.

Course illustration
Course illustration

All Rights Reserved.