Encode Keys
Encode Keys
You are given a list of string keys. Build: 1) unique: the distinct keys in the order they first appear, and 2) ids: an integer array where ids[i] is the index of keys[i] in unique.
Return both arrays.
Function signature
func EncodeKeys(keys []string) ([]string, []int)
Example
keys = ["us", "ca", "us", "mx", "ca"]
unique = ["us", "ca", "mx"]
ids = [0, 1, 0, 2, 1]
Constraints
- 0 <= len(keys) <= 200_000
- 1 <= len(keys[i]) <= 32
- keys contain ASCII letters, digits, or "_"
Notes
- Aim for O(n) time using a hash map.
- Preallocate slices to avoid repeated growth.
Run tests to see results
No issues detected