Least Frequent Element

Medium ⏱ 12 min 62% acceptance ★★★★☆ 4.3
Write a function least_frequent(items) that returns the element in list items that occurs the fewest times. Break ties by returning whichever qualifying element appears first in items. Use a dict to count occurrences.

Examples

Example 1
Input
least_frequent([1, 2, 2, 3, 3, 3, 4])
Output
1
Explanation

1 and 4 both occur once; 1 appears first.

Constraints

  • items is non-empty.

Topics

Dictionariesfrequency analysis

Companies

Deutsche BankIntel

Hints

Hint 1

Build a count dict first.

Hint 2

Then scan items in order and return the first whose count equals the minimum count.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.