Write Python code to compute the frequency of each number in the list `[1, 2, 2, 3, 3, 3, 4]`.
1Times asked
Sep 2026Last seen
Sep 2026First seen
💡 Model Answer
You can use a dictionary or collections.Counter to count occurrences. Here’s a simple implementation:
python
numbers = [1, 2, 2, 3, 3, 3, 4]
# Using a dictionary
freq = {}
for num in numbers:
freq[num] = freq.get(num, 0) + 1
print(freq)
# Using Counter
from collections import Counter
freq2 = Counter(numbers)
print(freq2)Both snippets output {1: 1, 2: 2, 3: 3, 4: 1}. The time complexity is O(n) and the space complexity is O(k) where k is the number of distinct elements.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500