create map of elements of array A1
Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { int a ; if ( m . ContainsKey ( A1 [ i ] ) ) { a = m [ A1 [ i ] ] + 1 ; m . Remove ( A1 [ i ] ) ; m . Add ( A1 [ i ] , a ) ; } else m . Add ( A1 [ i ] , 1 ) ; }