Home › Interview Questions › Python, Data Structures, Algorithms

Given the list list1 = [1, 2, 2, 3, 1, 4, 2, 3], find the frequency of each element without using existing functions.

🟢 Easy Coding Junior level
1 Times asked
Mar 2026 Last seen
Mar 2026 First seen

💡 Model Answer

To count frequencies without built‑in helpers, iterate over the list and use a dictionary to store counts. Initialize an empty dict. For each element, check if it exists in the dict; if so, increment its value, otherwise set it to 1. This runs in O(n) time and O(k) space, where n is the list length and k is the number of distinct elements. Example code:

freq = {}

for x in list1:

if x in freq:
    freq[x] += 1
else:
    freq[x] = 1

print(freq)

The output will be {1: 2, 2: 3, 3: 2, 4: 1}.

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 — invisible to screen sharing.

Get Assisting AI — Starts at ₹500