HomeInterview QuestionsExplain how to find duplicate numbers in an array …

Explain how to find duplicate numbers in an array using a counting approach.

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

💡 Model Answer

To find duplicate numbers in an array using a counting approach, iterate through the array and maintain a hash map (or dictionary) that records the frequency of each element. As you traverse, increment the count for each number. After the pass, iterate over the map and collect keys whose count is greater than one; these are the duplicates. This method runs in O(n) time and O(n) space, where n is the array length. In languages like Python, you can use collections.Counter to simplify: from collections import Counter; counts = Counter(arr); duplicates = [num for num, cnt in counts.items() if cnt > 1]. In Java, use a HashMap<Integer, Integer> to count occurrences. If the array values are bounded and small, you can use an auxiliary array of size max_value+1 to count frequencies, achieving O(n) time and O(k) space where k is the range. This counting technique is efficient for large datasets and avoids nested loops.

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