Inversion of Object - Problem

Given a table containing key-value pairs, create an inverted table where the original keys become values and the original values become keys. When multiple keys have the same value, the inverted table should group those keys together.

Requirements:

  • Transform keys to values and values to keys
  • Handle duplicate values by grouping the corresponding keys
  • All values are guaranteed to be strings
  • Array indices should be treated as keys

Table Schema

KeyValuePairs
Column Name Type Description
id PK int Primary key identifier
key_name varchar The original key or index
value_content varchar The original value content
Primary Key: id
Note: Each row represents a key-value pair from the original object or array

Input & Output

Example 1 — Simple Object Inversion
Input Table:
id key_name value_content
1 name John
2 age 25
3 city NYC
Output:
inverted_key inverted_value
25 age
John name
NYC city
💡 Note:

Each key-value pair is inverted: the original values become the new keys, and the original keys become the new values. Since all values are unique, no aggregation is needed.

Example 2 — Handling Duplicate Values
Input Table:
id key_name value_content
1 a hello
2 b world
3 c hello
Output:
inverted_key inverted_value
hello a,c
world b
💡 Note:

When multiple keys have the same value ('hello'), the inverted result groups those keys together using comma separation. The value 'hello' maps to both 'a' and 'c', so the result shows 'a,c'.

Example 3 — Array Indices as Keys
Input Table:
id key_name value_content
1 0 apple
2 1 banana
3 2 apple
Output:
inverted_key inverted_value
apple 0,2
banana 1
💡 Note:

Array indices are treated as keys. The value 'apple' appears at indices 0 and 2, so it maps to '0,2' in the inverted result. The value 'banana' appears only at index 1.

Constraints

  • 1 ≤ number of key-value pairs ≤ 1000
  • All values are guaranteed to be strings
  • Keys can be either string names or array indices

Visualization

Tap to expand
Inversion of Object INPUT Original Object "a": 1 "b": 2 "c": 1 "d": 3 "e": 2 Keys: a, b, c, d, e Values: 1, 2, 1, 3, 2 (Note: duplicate values) Structure key --> value One-to-one mapping ALGORITHM STEPS 1 Initialize Result Create empty object result = {} 2 Iterate Keys Loop through each key for key in obj 3 Get Value Extract current value val = obj[key] 4 Invert & Group Value becomes key, group original keys a:1 --> 1:[a] FINAL RESULT Inverted Object 1: ["a", "c"] 2: ["b", "e"] 3: ["d"] Grouped by original values OK - Inversion Complete Values are now keys Keys grouped in arrays Mapping Changed: key --> value value --> [keys] Key Insight: When inverting an object, multiple keys may share the same value. Instead of overwriting, we group all original keys into an array under their shared value. This preserves all data and creates a one-to-many mapping. Time Complexity: O(n) where n = number of keys. TutorialsPoint - Inversion of Object | Optimal Solution
Asked in
Meta 15 Google 12 Amazon 8
23.4K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen