Distant Barcodes LeetCode solution in JavaScript

1
var rearrangeBarcodes = function (barcodes) {
2
var result = [];
3
var map = new Map();
4
barcodes.forEach((n) => map.set(n, map.get(n) + 1 || 1));
5
let list = [...map.entries()].sort((a, b) => {
6
return b[1] - a[1];
7
});
8
let i = 0; //list[i][0]=>number list[i][1]=>count of this number
9
while (result.length !== barcodes.length) {
10
if (list[i][1] > 0) result.push(list[i][0]), list[i][1]--;
11
i++;
12
if (list[i] === undefined) i = 0;
13
if (
14
list[0][1] - list[i][1] >= 1 &&
15
result[result.length - 1] !== list[0][0]
16
)
17
i = 0;
18
} //list has sorted, so list[0] appeared most frequent
19
return result;
20
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0