BudiBadu Logo

Duplicate Indices Map

Hash Table Easy 32 views
Like27

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

Example 1
Input
nums = [1,2,3,1,2,1]
Output
[[0,3,5],[1,4]]
Explanation

Value 1 appears at indices 0, 3, 5; value 2 appears at indices 1, 4.

Example 2
Input
nums = [4,5,6]
Output
[]
Explanation

No duplicates exist.

Example 3
Input
nums = [7,7,7]
Output
[[0,1,2]]
Explanation

Value 7 repeats across all positions.

Algorithm Flow

Recommendation Algorithm Flow for Duplicate Indices Map - Budibadu
Recommendation Algorithm Flow for Duplicate Indices Map - Budibadu

Best Answers

java
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;
    }
}