Iterate from 0 to n - 1
for ( int i = 0 ; i < n ; i ++ ) { if ( hashmap . ContainsKey ( arr [ i ] ) ) { hashmap [ arr [ i ] ] . Add ( i ) ; } else { hashmap . Add ( arr [ i ] , new List < int > ( ) ) ; hashmap [ arr [ i ] ] . Add ( i ) ; } }