Skip to content

Instantly share code, notes, and snippets.

@rehangit
Last active April 30, 2016 19:15
Show Gist options
  • Save rehangit/a5b0a61900b468a7cc482ea203fce4b2 to your computer and use it in GitHub Desktop.
Save rehangit/a5b0a61900b468a7cc482ea203fce4b2 to your computer and use it in GitHub Desktop.
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(", ") + "]");
}
}
}
@rehangit
Copy link
Author

rehangit commented Apr 30, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment