To store max Index encountred so far
var index = - 1 ; for ( var j = 0 ; j < N ; ++ j ) { if ( S [ j ] === ch ) count ++ ; if ( count === K ) index = j ; } answer . push ( [ ch , index ] ) ; } var flag = 0 ;