Pop the back element from the vp until the first value is same as val and difference between indices is less than K
while ( vp . length > 0 && ( vp [ vp . length - 1 ] . first == val ) && ( ind - vp [ vp . length - 1 ] . second + 1 <= K ) ) { vp . pop ( ) ; } }