KatInterview

Find Most Common Elements Without Built-in Max

Arrays & HashingAuto-graded · 22 test cases
MediumAuto-gradedArrays & Hashinghash-mapfrequency-countingiteration

Last updated

Tests manual maximum-finding across a frequency dictionary, tie-breaking by first occurrence, and working with stringified keys.

Write a function that identifies which elements appear most frequently in a list, returning all elements tied for the highest count.

Your function receives a list of items (which may be of mixed types: integers, strings, booleans, None, etc.). You must:

  1. Convert each item to its string representation for comparison purposes
  2. Count how many times each stringified value appears
  3. Find the maximum frequency without using Python's max() function or collections.Counter
  4. Return all stringified keys that have that maximum frequency

Output Requirements:

  • Return a list of strings (the stringified keys)
  • When multiple keys tie for the highest frequency, return them in the order they first appeared in the input
  • If the input is empty, return an empty list

Constraints:

  • You may not use max(), Counter.most_common(), or similar built-in maximum-finding functions
  • You must find the highest count through explicit iteration
  • 0 ≤ len(items) ≤ 10⁴
  • Items may be of any type; treat them by their string representation
  • String conversion is case-sensitive: "park" and "Park" are distinct keys

Examples

Example 1

Input: find_most_common(['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park'])

Output: ['park', 'Park']

Example 2

Input: find_most_common([1, 2, 2, 3, 1])

Output: ['1', '2']

Example 3

Input: find_most_common([True, 'True', True, None, None])

Output: ['True']

Example 4

Input: find_most_common([])

Output: []

Hints

Hint 1

Build a dictionary mapping each stringified item to its count while tracking the order keys first appear

Hint 2

Loop through your frequency counts manually, keeping track of the highest value seen so far

Hint 3

When you find a new maximum, replace your result list; when you find a tie, append to it