To find the distance between any two occurences
for ( int i = 0 ; i < indices . get ( t ) . size ( ) ; i ++ ) { gap = Math . max ( gap , indices . get ( t ) . get ( i ) - start ) ; start = indices . get ( t ) . get ( i ) ; } if ( answer [ gap ] == - 1 ) answer [ gap ] = t ; }