|Count Occurrences
All Challenges

Count Occurrences

MediumDictionaries

Given a list lst, return a dictionary where each key is a unique item from the list and each value is how many times that item appears.


This is the backbone of word frequency counters, analytics pipelines, vote tallying systems, and inventory trackers.

Examples

Input: count_occurrences([1, 2, 2, 3, 3, 3])
Output: {1: 1, 2: 2, 3: 3}
Each number mapped to its count
Input: count_occurrences(["a", "b", "a", "c"])
Output: {"a": 2, "b": 1, "c": 1}
"a" appears twice
Input: count_occurrences([])
Output: {}
Empty list returns empty dict

Constraints

  • 0 <= len(lst) <= 100,000
  • List items are integers or strings
pythoneditor
Loading Python runtime…