LeetCode Records - Question 2822 Inversion of Object
/**
* @param {Object|Array} obj
* @return {Object}
*/
var invertObject = function(obj) {
answer = {};
for (let key of Object.keys(obj)) {
let val = obj[key];
if (answer.hasOwnProperty(val)) {
if (Array.isArray(answer[val])) {
answer[val].push(key);
} else {
answer[val] = [answer[val], key];
}
} else {
answer[val] = key;
}
}
return answer;
};
- Runtime: 144 ms (Beats: 95.35%)
- Memory: 64.12 MB (Beats: 67.44%)