Inversion of Object - Solution & Explanation
Problem Statement
Given an object or an array obj, return an inverted object or array invertedObj.
The invertedObj should have the keys of obj as values and the values of obj as keys. The indices of array should be treated as keys.
The function should handle duplicates, meaning that if there are multiple keys in obj with the same value, the invertedObj should map the value to an array containing all corresponding keys.
It is guaranteed that the values in obj are only strings.
Example 1:
Input: obj = {"a": "1", "b": "2", "c": "3", "d": "4"}
Output: invertedObj = {"1": "a", "2": "b", "3": "c", "4": "d"}
Explanation: The keys from obj become the values in invertedObj, and the values from obj become the keys in invertedObj.
Example 2:
Input: obj = {"a": "1", "b": "2", "c": "2", "d": "4"}
Output: invertedObj = {"1": "a", "2": ["b", "c"], "4": "d"}
Explanation: There are two keys in obj with the same value, the invertedObj mapped the value to an array containing all corresponding keys.
Example 3:
Input: obj = ["1", "2", "3", "4"]
Output: invertedObj = {"1": "0", "2": "1", "3": "2", "4": "3"}
Explanation: Arrays are also objects therefore array has changed to an object and the keys (indices) from obj become the values in invertedObj, and the values from obj become the keys in invertedObj.
Constraints:
objis a valid JSON object or arraytypeof obj[key] === "string"2 <= JSON.stringify(obj).length <= 105
Approach Overview
Problem Overview: You are given a JavaScript object where each key maps to a value. The task is to invert the object so that values become keys and the original keys become values. If multiple keys share the same value, the inverted object should store those keys in an array.
Approach 1: Naive Grouping with Array Search (O(n²) time, O(n) space)
A straightforward way is to iterate through every key–value pair and build the inverted structure manually. For each value, check whether the inverted object already contains that value as a key. If it does, push the current key into the stored array. If not, create a new array with the current key. If implemented inefficiently with repeated scans or conversions (for example repeatedly searching existing arrays or keys), the process can degrade toward O(n²) time. Space complexity remains O(n) because the inverted object stores all keys once.
Approach 2: Hash Map Inversion Using Object Iteration (O(n) time, O(n) space)
The optimal solution treats the result as a hash map. Iterate through the object using Object.entries() or a for...in loop. For each pair (key, value), check whether the value already exists in the result object. If not, initialize an array. Then append the current key to that array. Each insertion and lookup in a JavaScript object works like a constant-time hash table operation, giving an overall time complexity of O(n) where n is the number of keys in the original object.
This approach is clean and predictable. Every element is processed exactly once. The inverted structure naturally groups duplicate values, which is the key requirement of the problem. Space complexity stays O(n) because the result stores all original keys across arrays.
Conceptually, this problem is a simple application of hash table usage and object traversal in JavaScript. Understanding how to iterate through key–value pairs using JavaScript objects or hash maps is the core skill being tested.
Recommended for interviews: The hash map iteration approach. Interviewers expect you to recognize that the inversion naturally maps to grouping keys by value. The naive idea demonstrates understanding of the transformation, but the O(n) hash map solution shows that you know how to leverage constant-time lookups and build grouped results efficiently.
Solution
Code
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Naive Grouping with Array Search | O(n²) | O(n) | Educational baseline or when implementing grouping logic without hash optimizations |
| Hash Map Inversion Using Object Iteration | O(n) | O(n) | General case and interview-preferred approach using constant-time lookups |
Video Solution
Junior Software Dev vs Senior Dev solving Valid Anagram • Greg Hogg • 693,801 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Inversion of Object easy or hard?
Inversion of Object Python/Java solution
How to solve Inversion of Object in O(n)?
What is the best approach for Inversion of Object?
Is Inversion of Object asked at Google/Amazon/Meta?
What data structure is used in Inversion of Object?
What is the time complexity of Inversion of Object?
Ready to solve this problem?
Practice Inversion of Object with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor