Duplicate Indices Map
In Duplicate Indices Map, your goal is to find all transaction IDs that appear more than once and return a dictionary mapping each duplicate ID to a list of its indices in the original ledger. This helps the audit team spot double-billing instantly!
The "secret sauce" is a Frequency Dictionary. As you walk through the array, you store each number as a key and its index in a list of values. Afterward, you filter the map to keep only the keys with more than one index. This approach provides an efficient O(n) solution, avoiding manual nested searches. It’s a classic data-processing pattern that turns a flat list into a detailed map of overlaps! Empty ledgers or those with only unique IDs return an empty dictionary correctly.
When the task involves connectivity or route cost, build adjacency carefully and guard against revisiting stale states. Use a visited or best-distance structure to avoid repeated work, and ensure unreachable scenarios return the required fallback value instead of partial traversal results.
Examples
Value 1 appears at indices 0, 3, 5; value 2 appears at indices 1, 4.
No duplicates exist.
Value 7 repeats across all positions.
Algorithm Flow

Best Answers
import java.util.*;
class Solution {
public List<List<Integer>> duplicate_indices_map(int[] nums) {
Map<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
}
List<List<Integer>> result = new ArrayList<>();
for (List<Integer> indices : map.values()) {
if (indices.size() > 1) {
result.add(indices);
}
}
return result;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this problem.
