Frequency Count Using Only Lists

Medium ⏱ 12 min 61% acceptance ★★★★☆ 4.2
Write most_frequent_value(items) that returns the value appearing most often in items without using a dictionary or <code>collections.Counter</code> — use two parallel plain lists (one for unique values seen, one for their counts) to track frequencies. If there is a tie, return whichever tied value appears first in items. Assume items is non-empty.

Examples

Example 1
Input
items = ['x', 'y', 'x', 'z', 'x', 'y']
Output
'x'
Explanation

'x' appears 3 times, more than any other value.

Constraints

  • 1 <= len(items) <= 10^4

Topics

ListsFrequency Counting

Companies

ZomatoRazorpay

Hints

Hint 1

Keep a values list and a matching counts list at the same indices.

Hint 2

For each item, find it in values (or add it) and bump the matching counts entry.

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