Maps to store frequencies of elements present in left and right arrays
Dictionary < int , int > freq1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > freq2 = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq1 . ContainsKey ( left [ i ] ) ) freq1 [ left [ i ] ] ++ ; else freq1 [ left [ i ] ] = 1 ; if ( freq2 . ContainsKey ( right [ i ] ) ) freq2 [ right [ i ] ] ++ ; else freq2 [ right [ i ] ] = 1 ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) {