Count occurrences of all remainders
for ( let i = 0 ; i < ar . length ; i ++ ) { let rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . has ( rem ) ) { hm . set ( rem , 0 ) ; } hm . set ( rem , hm . get ( rem ) + 1 ) ; }