S2Q2 · Most Frequent Numbers from Input¶
⚡ Quick Reference
Type: Full I/O problem
Core idea: count frequencies, find the maximum frequency, collect all numbers with that frequency, sort and print as a list.
n = int(input())
nums = [int(input()) for _ in range(n)]
freq = {}
for x in nums:
freq[x] = freq.get(x, 0) + 1
max_freq = max(freq.values())
result = sorted(k for k, v in freq.items() if v == max_freq)
print(result)
Key rules:
- Read n, then n numbers one per line
- Find the maximum frequency among all numbers
- Collect all numbers tied at that maximum frequency
- Print sorted in ascending order as a Python list
Problem Statement¶
Problem (I/O type)
Read n numbers and print the most frequent one(s) in ascending order as a list. If multiple numbers share the maximum frequency, include all of them.
Example:
10
3
5
3
2
5
8
3
8
5
0
[3, 5]
Understanding the problem¶
Two steps:
- Count how many times each number appears
- Find all numbers whose count equals the maximum count
Numbers: [3, 5, 3, 2, 5, 8, 3, 8, 5, 0]
Frequency table:
3 → 3 times ← maximum
5 → 3 times ← maximum
2 → 1 time
8 → 2 times
0 → 1 time
max_freq = 3
Result = [3, 5] (sorted ascending)
Why collect all tied numbers?
The problem asks for the most frequent numbers (plural). If two numbers share the same maximum frequency - as 3 and 5 do here - both must be included in the output.
Building the frequency table¶
Three equivalent approaches:
# Option A - dict.get()
freq = {}
for x in nums:
freq[x] = freq.get(x, 0) + 1
# Option B - defaultdict
from collections import defaultdict
freq = defaultdict(int)
for x in nums:
freq[x] += 1
# Option C - Counter (most compact)
from collections import Counter
freq = Counter(nums)
All three produce the same result. Counter is the most Pythonic for frequency counting.
Tracing the example¶
nums = [3, 5, 3, 2, 5, 8, 3, 8, 5, 0]
| Number | Count |
|---|---|
| 3 | 3 |
| 5 | 3 |
| 2 | 1 |
| 8 | 2 |
| 0 | 1 |
max_freq = max({3:3, 5:3, 2:1, 8:2, 0:1}.values()) = 3
Numbers with count == 3: [3, 5] → sorted → [3, 5]
Output: [3, 5]
Solution approaches¶
from collections import Counter
n = int(input())
nums = [int(input()) for _ in range(n)]
freq = Counter(nums)
max_freq = max(freq.values())
result = sorted(k for k, v in freq.items() if v == max_freq)
print(result)
Counter handles the frequency table in one call. The generator expression filters and sorted() orders the result.
from collections import Counter
n = int(input())
nums = [int(input()) for _ in range(n)]
freq = Counter(nums)
max_freq = max(freq.values())
result = sorted(filter(lambda k: freq[k] == max_freq, freq))
print(result)
filter(lambda k: freq[k] == max_freq, freq) iterates over the dict keys and keeps only those whose frequency equals the maximum. sorted() wraps it to produce the final ordered list.
from collections import Counter
n = int(input())
nums = [int(input()) for _ in range(n)]
freq = Counter(nums)
max_freq = freq.most_common(1)[0][1] # frequency of the most common element
result = sorted(k for k, v in freq.items() if v == max_freq)
print(result)
Counter.most_common(1) returns the single most frequent (element, count) pair. Index [0][1] extracts just the count. Avoids calling max() separately.
Key takeaways¶
Counter for frequency tables
Counter(iterable) builds a frequency dict in one call. It also supports .most_common(n) to get the top-n elements by frequency.
Collect all ties, not just one
Always filter for all elements matching max_freq. Using max(freq, key=freq.get) returns only one element - it misses ties.
print(list) vs print(*list)
print([3, 5]) outputs [3, 5] - with brackets, as required here. print(*[3, 5]) outputs 3 5 - no brackets. Match the expected output format carefully.