Find Most Common Elements Without Built-in Max
Arrays & HashingAuto-graded · 22 test casesLast 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:
- Convert each item to its string representation for comparison purposes
- Count how many times each stringified value appears
- Find the maximum frequency without using Python's
max()function orcollections.Counter - 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
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core challenge is finding the maximum frequency without using built-in functions like max(). We solve this in three steps:
-
Build a frequency map: Iterate through the input once, converting each item to a string and counting occurrences in a dictionary. Simultaneously maintain a separate list tracking the order keys first appear.
-
Find the maximum count manually: Loop through all frequency values, maintaining a running variable for the highest count seen so far. Compare each count against this variable and update it when we find something larger.
-
Collect all keys matching the maximum: Filter the ordered key list to include only those whose frequency equals the maximum we found.
Why this preserves first-appearance order: We track keys in a separate order list as we encounter them for the first time. When filtering for maximum-frequency keys, we iterate through this ordered list, which naturally gives us the correct sequence.
Edge cases handled:
- Empty input returns an empty list (the counts dictionary is empty, so we short-circuit)
- All unique elements all tie at frequency 1, so all are returned in input order
- Mixed types like
1and"1"are treated as distinct after stringification (one becomes"1", the other is already"1")
Complexity
-
Time: O(n) where n is the length of the input. We make three linear passes: one to build counts, one to find the maximum, and one to filter results. The distinct number of keys k ≤ n, so even the filtering step is bounded by n.
-
Space: O(k) where k is the number of distinct stringified keys, for storing the frequency dictionary and order list. In the worst case where all items are unique, k = n.
Solution
from typing import List
def find_most_common(items: List) -> List[str]:
counts = {}
order = []
for item in items:
key = str(item)
if key not in counts:
counts[key] = 1
order.append(key)
else:
counts[key] += 1
if not counts:
return []
highest = 0
for count in counts.values():
if count > highest:
highest = count
return [key for key in order if counts[key] == highest]