Snack Vote Winner with Counter()
A class is choosing one snack for a movie afternoon. Every student writes down a single snack name, and all of those votes are collected into one list. The teacher now wants to know which snack won the vote.
Your task is to return the snack name with the highest number of votes. If more than one snack has the same highest count, return the alphabetically smaller name so the tie is resolved in a consistent way.
For example, if the votes are ["chips", "juice", "chips", "cookie"], the answer should be "chips" because it appears twice while the others appear less often. If the votes are ["apple", "banana", "banana", "apple"], both snacks have the same count, so the answer should be "apple" because it comes first alphabetically.
If the list is empty, return an empty string "". This challenge is about counting votes, comparing totals, and applying a small tie-breaking rule at the end.
Example Input & Output
No votes means there is no winning snack name to return.
The vote count is tied, so the alphabetically smaller snack is returned.
Chips has the highest frequency, so it wins the vote.
Algorithm Flow

Solution Approach
This problem has two parts. First, you need to count how many times each snack appears. Second, you need to choose the best answer using the highest count and, if needed, alphabetical order as the tie-breaker. Python's Counter() from collections is a very good fit for the counting step.
Once the votes are inside a Counter, every snack name is already mapped to its total number of votes. That means the counting work becomes very small:
After that, you can look for the largest vote total and collect the snack names that reached it. If more than one name shares that top count, returning min(winners) gives the alphabetically smaller one.
One clear version is:
This makes each step easy to follow. First handle the empty case, then count, then identify the highest total, and finally apply the tie rule only among the top snacks.
You can also compress the final selection with a custom comparison key, but the structure above keeps the reasoning visible. The important data-structure idea is that Counter is built exactly for this kind of frequency-based decision. It saves you from manually initializing and updating a plain dictionary.
The runtime is O(n + k) for n votes and k distinct snack names, and the extra space is proportional to the number of distinct names stored in the counter.
Best Answers
from collections import Counter
def snack_vote_winner(votes):
if not votes:
return ""
counts = Counter(votes)
best_count = max(counts.values())
winners = [snack for snack, count in counts.items() if count == best_count]
return min(winners)Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
