Find All Top-Frequency Labels Without Using max()
Arrays & HashingAuto-graded · 21 test casesLast updated
Count items by their string form and return every label tied for the highest count, in first-appearance order, without calling max().
Top-Frequency Labels
You are given a list of items, which may be of mixed types. Convert each item to text with Python's str() and count how many times each resulting text label occurs. Return all labels that occur the most often.
Write a function that takes the list and returns the list of labels with the highest count.
Rules
- Every item is converted with
str(item)before counting, so counting is case-sensitive. For example,"park"and"Park"are different labels. Values of different types that produce the same text, such as1and"1", count as the same label. - If several labels share the highest count, return all of them.
- Labels in the result must appear in order of their first appearance in the input list.
- If the input list is empty, return an empty list.
- You may not use the built-in
max()function. Find the highest count yourself. - The return value is a list of strings.
Constraints
0 <= len(items) <= 10^5- Each item can be converted to text with
str(). - Do not use
max().
Examples
Example 1
Input: top_frequency_labels(['park', 'Park', 'McDonalds', 'apple', 'Apple Park', 'park', 'Park'])
Output: ['park', 'Park']
Example 2
Input: top_frequency_labels(['a', 'b', 'a', 'c', 'b'])
Output: ['a', 'b']
Example 3
Input: top_frequency_labels([])
Output: []
Hints
Hint 1
Build a dictionary that maps each text label to the number of times it occurs. Python dictionaries remember insertion order, which helps with the ordering rule.
Hint 2
Replace max() with a loop: keep a variable for the highest count seen so far and update it whenever you see a larger count.
Hint 3
Once you know the highest count, make a second pass over the dictionary and collect every label whose count equals it. An empty input leaves the highest count at 0 and no labels are collected, so it returns [].
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- Count. Walk through the input once. Convert each item with
str()and increment its count in a dictionary. Since Python 3.7, dictionaries keep insertion order, so the keys end up in order of first appearance. - Empty check. If the dictionary is empty, return
[]. - Find the highest count manually. Start a variable
highestat0. Every real count is at least 1, so the first count always replaces it. Loop over the counts and updatehighestwhenever a larger count appears. This is the loop thatmax()would hide. - Collect ties. Walk through the dictionary again and append every key whose count equals
highest. Because the dictionary is ordered by first appearance, the result is too.
Why it is correct
After step 3, highest is the largest value among all counts, since each count is compared against the running best and the best is only ever raised. Step 4 then selects exactly the labels whose count equals that value, so ties are all included and nothing else is. Converting with str() before counting gives the required case-sensitive behavior and merges values that share the same text. For an empty input, no counts exist and the function returns [].
Complexity
- Time: O(n) to count, plus O(k) for the two passes over the k distinct labels, which gives O(n) overall.
- Space: O(k) for the dictionary and the result.
Reference solution
from typing import List
def top_frequency_labels(items: List[object]) -> List[str]:
counts = {}
for item in items:
key = str(item)
if key in counts:
counts[key] += 1
else:
counts[key] = 1
if not counts:
return []
highest = 0
for count in counts.values():
if count > highest:
highest = count
result = []
for key, count in counts.items():
if count == highest:
result.append(key)
return result