Insert elements of both arrays
Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . ContainsKey ( A [ i ] ) ) { var v = hash [ A [ i ] ] ; hash . Remove ( A [ i ] ) ; hash . Add ( A [ i ] , 1 + v ) ; } else hash . Add ( A [ i ] , 1 ) ; if ( hash . ContainsKey ( B [ i ] ) ) { var v = hash [ B [ i ] ] ; hash . Remove ( B [ i ] ) ; hash . Add ( B [ i ] , 1 + v ) ; } else hash . Add ( B [ i ] , 1 ) ; }