Mapping index with their values in ordered map
for ( int i = 0 ; i < n ; i ++ ) { List < Integer > indexes ; if ( m . containsKey ( arr [ i ] ) ) { indexes = m . get ( arr [ i ] ) ; } else { indexes = new ArrayList < Integer > ( ) ; } indexes . add ( i ) ; m . put ( arr [ i ] , indexes ) ; }