Skip to content

Commit c5cb167

Browse files
authored
Create 1054-distant-barcodes.js
1 parent c6aee05 commit c5cb167

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

1054-distant-barcodes.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} barcodes
3+
* @return {number[]}
4+
*/
5+
const rearrangeBarcodes = function(barcodes) {
6+
const map = {};
7+
barcodes.forEach(b => map[b] = (map[b] || 0) + 1);
8+
const keys = Object.keys(map).sort((k1, k2) => map[k1] - map[k2]);
9+
10+
let idx = 1;
11+
for (let k of keys) {
12+
let t = map[k];
13+
14+
for (let i = 0; i < t; i++) {
15+
if (idx >= barcodes.length) idx = 0;
16+
barcodes[idx] = k;
17+
idx += 2;
18+
}
19+
}
20+
21+
return barcodes;
22+
};

0 commit comments

Comments
 (0)