Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 17

Count Item Frequencies

Medium

15 minute session

Summary

Return a dictionary mapping each distinct item to how many times it appears in a list.

Problem statement

Implement a function that takes a list of items and returns a dictionary where each key is an item from the list and its value is the number of times that item appears. Maintain the order of first appearance for keys (the natural behavior of Python dict in modern versions will satisfy this). The function should handle empty lists and arbitrary hashable items (numbers, strings, tuples, booleans). This exercise reinforces looping, dictionary usage, and counting patterns.

Task

Practice iterating over a sequence and building a frequency table (dictionary) to count occurrences of items.

Examples

Count numbers

Input

[1, 2, 1, 3, 2]

Output

{1: 2, 2: 2, 3: 1}

Explanation

1 appears twice, 2 appears twice, 3 appears once.

Input format

A single list of hashable items passed to count_frequencies(items).

Output format

A dictionary mapping each distinct item to its count (int).

Constraints

The list length will be between 0 and 10,000. Items are hashable. Aim for O(n) time and O(k) additional space where k is number of distinct items.

Samples

Sample input 0

["a", "b", "a"]

Sample output 0

{'a': 2, 'b': 1}

Explanation 0

Counts for each string item.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:45 AM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.