Find duplicate values in javascript associative array (hash) -


have key value pair below

var point = []; point["i1"] = 1000; point["i2"] = 2000; point["i3"] = 1000; point["i4"] = 5000; point["i5"] = 2000; point["i6"] = 4000; 

want find duplicate values, result hash should contain duplicate value information.

result["1000"] = " i1,i3"; result["2000"] = "i2,i5"; 

duplicate values should keys of result hash.

i sort array, clueless traverse hash check duplicate values build result hash

https://fiddle.jshell.net/jbs9y896/1/

you can check values loop on keys of point. changed terms little bit. can set keys array you'd better use object instead. result each value array can converted comma separated values tostring call on each array.

edit

as request edited, contain only duplicates can loop again (but time on hash) , has more 1 result.

var point = {    i1: 1000,    i2: 2000,    i3: 1000,    i4: 5000,    i5: 2000,    i6: 4000,  };    var hash = object.create(null);  var result = object.create(null);    object.keys(point).foreach(k => {    var grp = point[k];    (grp in hash) ? hash[grp].push(k): (hash[grp] = [k]);  });    (key in hash)    if (hash[key].length > 1)      result[key] = hash[key].tostring();    console.log(hash['1000'].tostring());  console.log(hash);  console.log(result);


Comments