Function to sort according to bit count . This function assumes that there are 32 bits in an integer .
function sortBySetBitCount ( arr , n ) { let count = new Array ( 32 ) ; for ( let i = 0 ; i < count . length ; i ++ ) count [ i ] = [ ] ; let setbitcount = 0 ; for ( let i = 0 ; i < n ; i ++ ) { setbitcount = countBits ( arr [ i ] ) ; count [ setbitcount ] . push ( arr [ i ] ) ; }