Deduplicate Coordinate List with Index Mapping
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests the ability to extract distinct elements while preserving first-occurrence order and building an index array for reconstruction.
You're building a data processing pipeline that works with sequences of 3D coordinates. Each coordinate is a list of three numbers [x, y, z]. The input often contains many duplicate coordinates, wasting memory.
Write a function that compresses the coordinate list by:
- Extracting all distinct coordinates in the order they first appear
- Building an index array where each position points to the corresponding unique coordinate
The function should return a list containing exactly two elements:
- First element: a list of unique coordinates (each as a list of three numbers)
- Second element: a list of integer indices
The indices must allow perfect reconstruction: for any position i in the original list, unique_coords[index_array[i]] equals original[i].
Constraints
- Input length: 1 ≤ n ≤ 1,000,000
- Each coordinate contains exactly three numeric values
- Coordinates are considered equal if all three values match exactly
- Standard floating-point equality applies (IEEE 754)
Examples
Example 1
Input: deduplicate_coordinates([[1, 2, 3], [4, 5, 6], [1, 2, 3]])
Output: [[[1, 2, 3], [4, 5, 6]], [0, 1, 0]]
Example 2
Input: deduplicate_coordinates([[0, 0, 0], [0, 0, 0]])
Output: [[[0, 0, 0]], [0, 0]]
Hints
Hint 1
Use a dictionary to track which coordinates you've already seen and their assigned indices
Hint 2
Tuples are hashable in Python and can serve as dictionary keys, while lists cannot
Hint 3
Process coordinates in a single pass, assigning each new unique coordinate the next available index
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires building a mapping from duplicate elements to a canonical set of unique values. The core technique is maintaining a hash map that tracks which coordinates have been encountered and their position in the unique list.
Walk through the input once. For each coordinate:
- Convert it to a tuple so it can be used as a dictionary key (lists aren't hashable)
- Check if this tuple exists in the dictionary
- If new: assign it the next index (the current length of the unique list), add the coordinate to the unique list, and store the mapping
- If seen before: retrieve its previously assigned index
- Append the index to the output array
This ensures that the unique list grows only when encountering new coordinates and preserves first-occurrence order. The index array records which unique coordinate corresponds to each position in the original input.
Why It Works
Two properties hold throughout the scan:
-
Order preservation: Each coordinate is added to the unique list exactly once, at its first occurrence. Since we scan left-to-right and append only on first encounters, the unique list maintains first-appearance order.
-
Correct reconstruction: For every position processed, the index stored points to the correct unique coordinate. When a coordinate first appears, we append it to the unique list at position
len(unique_coords)and store that index in the map. All subsequent occurrences retrieve this same index from the map, which continues to point to the same coordinate since the unique list only grows at the end.
These properties guarantee that unique_coords[index_map[i]] == coords[i] for all positions.
Complexity
Time: O(n) where n is the number of coordinates. Each coordinate requires one dictionary lookup or insertion, which averages O(1). Hashing a tuple of three values is constant work.
Space: O(u) for the dictionary and unique list, where u is the number of distinct coordinates. The index array adds O(n). Total space is O(n + u). In the best case (all duplicates), u is small. In the worst case (all unique), u = n.
from typing import List
def deduplicate_coordinates(coords: List[List[float]]) -> List[List]:
unique_coords = []
index_map = []
coord_to_idx = {}
for coord in coords:
key = tuple(coord)
if key not in coord_to_idx:
coord_to_idx[key] = len(unique_coords)
unique_coords.append(list(coord))
index_map.append(coord_to_idx[key])
return [unique_coords, index_map]