Count elements of a
for ( int i = 0 ; i < n ; i ++ ) { if ( countA . ContainsKey ( a [ i ] ) ) { var v = countA [ a [ i ] ] ; countA . Remove ( countA [ a [ i ] ] ) ; countA . Add ( a [ i ] , v + 1 ) ; } else countA . Add ( a [ i ] , 1 ) ; }