1
var rankTeams = function (votes) {
2
if (votes.length == 1) return votes[0];
3
let map = new Map();
4
for (let vote of votes) {
5
for (let i = 0; i < vote.length; i++) {
6
if (!map.has(vote[i])) {
7
//create all the values set as zero
8
map.set(vote[i], Array(vote.length).fill(0));
9
}
10
let val = map.get(vote[i]);
11
val[i] = val[i] + 1;
12
map.set(vote[i], val);
13
}
14
}
15

16
let obj = [...map.entries()]; //converting as array ["A",[5,0,0]]
17
obj = obj.sort((a, b) => {
18
for (let i = 0; i < a[1].length; i++) {
19
if (a[1][i] > b[1][i]) return -1;
20
else if (a[1][i] < b[1][i]) return 1;
21
}
22
// if all chars are in same positon return the value charcode
23
return a[0].charCodeAt(0) - b[0].charCodeAt(0);
24
});
25
return obj.map((item) => item[0]).join("");
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0