Is there a better way to convert
["88 99", "20 99", "12 12"]
to a hashmap in the form
{"88": 1, "99": 2, "20": 1, "12": 1}
Using map or reduce?
Where in this case, a string with duplicate numbers only gets increases it's count by 1.
Currently I'm converting the above array into a 2d array using .split(' ')
and iterating over that 2d array in another for loop as so:
var counts = {}
for (let i = 0; i < logs.length; i++){
let ack = logs[i].split(' ');
if(ack[0]==ack[1]){
counts[ack[0]] = counts[ack[0]] ? counts[ack[0]] + 1 : 1;
}
else{
for(let j= 0; j < 2; j++){
counts[ack[j]] = counts[ack[j]] ? counts[ack[j]] + 1 : 1;
}
}
}
{"88": 1, "99": 2, "20": 1, "12": 2}