This is a premium problem. We're working on making it available for free soon.
Explore Free ProblemsSolutions for this premium problem will be available for free soon.
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, variations of object or hash map inversion appear in interviews to test understanding of hash maps, iteration, and handling duplicates. It is a common easy-level question that checks fundamental data structure skills.
The optimal approach is to iterate through the object's key-value pairs and construct a new object where values become keys and keys become values. If multiple keys share the same value, store them in an array to avoid overwriting data.
If multiple keys have the same value, the inverted object should store those keys in an array under that value. Each time the value appears again, simply append the key to the existing array.
A hash map (or standard object/dictionary) works best because it allows constant-time insertion and lookup. This makes it easy to check whether a value already exists as a key in the inverted structure.