Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Build a frequency dictionary and return the most common item

Count how often items appear and return the most frequent one, with a clear tie-breaker.

Python practice24 minDictionaries & SetsAdvancedLast updated March 18, 2026

Problem statement

Write a function most_common_item(items) that takes an iterable (like a list or tuple) of hashable items and returns the item that occurs most frequently. If the iterable is empty, return None. If two or more items have the same highest frequency, return the item among them that appears earliest in the original iterable. Do not use collections.Counter; implement the counting and tie-breaking logic yourself.

Task

Given an iterable of hashable items, build a frequency mapping and return the item with the highest frequency. If multiple items tie, return the one that appears first in the input.

Examples

Basic example

Input

most_common_item([1, 2, 2, 3])

Output

2

2 appears twice, more than any other item.

Input format

A single iterable value (e.g., a list) passed as the argument to most_common_item.

Output format

Return the most common item (any hashable type). If input is empty, return None.

Constraints

- Items are hashable (can be used as dictionary keys). - The input may be large but fits in memory. - Do not use collections.Counter; build the frequency mapping manually.

Samples

Sample 1

Input

most_common_item(['a', 'b', 'a', 'c', 'b', 'a'])

Output

a

a appears 3 times, more than any other item.