Decrease frequency and push back into priority queue if non - zero frequency
for ( var x = 0 ; x < 3 ; x ++ ) { ar [ x ] [ 0 ] -- ; if ( ar [ x ] [ 0 ] ) pq . push ( ar [ x ] ) ; } pq . sort ( ( a , b ) => { if ( a [ 0 ] == b [ 0 ] ) return a [ 1 ] - b [ 1 ] ; return a [ 0 ] - b [ 0 ] ; } ) ; }