Store last occurrence index of every element
Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . ContainsKey ( a [ i ] ) ) { hash [ a [ i ] ] = i ; } else { hash . Add ( a [ i ] , i ) ; } }