1
var sortByBits = function (arr) {
2
const map = {};
3

4
for (let n of arr) {
5
let counter = 0,
6
item = n;
7

8
while (item > 0) {
9
counter += item & 1; //increment counter if the lowest (i.e. the rightest) bit is 1
10
item = item >> 1; //bitwise right shift (here is equivalent to division by 2)
11
}
12

13
map[n] = counter;
14
}
15

16
return arr.sort((a, b) => map[a] - map[b] || a - b); //sort by number of 1 bits; if equal, sort by value
17
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0