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