HomeInterview QuestionsGiven a list of integer IDs, return all IDs that a…

Given a list of integer IDs, return all IDs that appear more than once. Return each duplicate only once, in the order it first becomes a duplicate. Example: [1,2,3,4,2,5,1].

🟡 Medium Coding Junior level
1Times asked
Jul 2026Last seen
Jul 2026First seen

💡 Model Answer

To solve this, iterate through the list while maintaining two sets: one for seen IDs and one for duplicates. For each ID, if it is not in seen, add it to seen. If it is already in seen but not yet in duplicates, add it to duplicates. After the loop, convert the duplicates set to a list preserving insertion order (e.g., by using an ordered set or a list that you append to when first encountering a duplicate). This yields the IDs that appear more than once, each listed only once, in the order of their first duplicate occurrence. Complexity is O(n) time and O(n) space, where n is the length of the list. Example Python code:

python
def find_duplicates(nums):
    seen = set()
    duplicates = []
    for num in nums:
        if num in seen:
            if num not in duplicates:
                duplicates.append(num)
        else:
            seen.add(num)
    return duplicates

print(find_duplicates([1,2,3,4,2,5,1]))  # Output: [2,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 on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500