-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2822-inversion-of-object.js
More file actions
38 lines (34 loc) · 1021 Bytes
/
2822-inversion-of-object.js
File metadata and controls
38 lines (34 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 2822. Inversion of Object
* https://leetcode.com/problems/inversion-of-object/
* Difficulty: Easy
*
* 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.
*/
/**
* @param {Object|Array} obj
* @return {Object}
*/
var invertObject = function(obj) {
const result = {};
Object.keys(obj).forEach(key => {
const value = obj[key];
if (result[value] === undefined) {
result[value] = key;
} else if (Array.isArray(result[value])) {
result[value].push(key);
} else {
result[value] = [result[value], key];
}
});
return result;
};