703872. Repeated IDs

Basic

Repeated IDs

Summary

The problem involves finding repeated IDs in a given array. The goal is to identify elements that appear more than once in the array. This problem is based on arrays and data structures, and requires an algorithmic approach to solve.

Detailed Explanation

To solve this problem, we can use a simple approach involving a hash map (or dictionary). We iterate through the array, and for each element, check if it's already present in the hash map. If it is, increment its count. If not, add it to the hash map with a count of 1.

Here's a step-by-step breakdown:

  1. Initialize an empty hash map idMap.
  2. Iterate through the array.
  3. For each element, check if it's already present in idMap. If it is:
    • Increment its count by 1.
  4. If not, add it to idMap with a count of 1.
  5. After iterating through the entire array, iterate through idMap.
  6. For each entry, if the count is greater than 1, print the ID as repeated.

Time complexity: O(n), where n is the length of the array. This is because we're iterating through the array once and then again for the hash map. Space complexity: O(n) for storing the hash map.

Here's an ASCII art diagram to illustrate the process:

          +---------------+
          |  Initialize   |
          +---------------+
                  |
                  v
+-----------------------+
| Iterate through array  |
+-----------------------+
                  |
                  v
+---------------+
| Check if ID in map    |
| (Increment count if   |
|  already present)     |
+---------------+
                  |
                  v
+-----------------------+
| Add ID to map with    |
| count = 1 if not      |
| present               |
+-----------------------+
                  |
                  v
+---------------+
| Iterate through map    |
| Print IDs with count > 1|
+---------------+

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.HashMap;
import java.util.Map;

public class RepeatedIDs {
    public static void findRepeated(int[] arr) {
        Map<Integer, Integer> idMap = new HashMap<>();
        
        for (int num : arr) {
            if (idMap.containsKey(num)) {
                idMap.put(num, idMap.get(num) + 1);
            } else {
                idMap.put(num, 1);
            }
        }
        
        for (Map.Entry<Integer, Integer> entry : idMap.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey());
            }
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.