To store the count of each a [ i ] in a map
Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( cnt . ContainsKey ( arr [ i ] ) ) { cnt [ arr [ i ] ] = cnt [ arr [ i ] ] + 1 ; } else { cnt . Add ( arr [ i ] , 1 ) ; } }