How would you determine if a list contains duplicate values?
1Times asked
Sep 2026Last seen
Sep 2026First seen
💡 Model Answer
A simple O(n) solution uses a hash set. Iterate over the list, adding each element to a set. If an element is already present in the set, you have found a duplicate and can return True. If you finish the loop without finding a duplicate, return False. In Python:
python
def has_duplicates(lst):
seen = set()
for x in lst:
if x in seen:
return True
seen.add(x)
return FalseThe set provides O(1) average‑time lookups, so the overall time complexity is O(n) and the space complexity is O(n).
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