Last active
April 30, 2016 19:15
-
-
Save rehangit/a5b0a61900b468a7cc482ea203fce4b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function invertKeyValues(obj) { | |
var inv = {}, | |
val; | |
for(var i in obj) { | |
val = obj[i]; | |
inv[val] = inv[val] || [] ; | |
inv[val].push(i); | |
} | |
return inv; | |
} | |
function test() { | |
var repeated = invertKeyValues({ | |
a: 1, | |
b: 2, | |
c: 3, | |
d: 1, | |
e: 0, | |
f: 2, | |
g: 0, | |
h: 1 | |
}); | |
for(var j in repeated) { | |
if(repeated[j].length>1) { | |
console.log("value: '" + j + "' repeats in keys: [" + repeated[j].join(", ") + "]"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find Duplicate Values in an Object
Given an object with key value pairs, this simple function allows one to find the keys which have duplicated values.