text: Count elements of same value placed at same indices of two given arrays | C # program for the above approach ; Function to count maximum matched elements from the arrays A [ ] and B [ ] ; Stores position of elements of array A [ ] in the array B [ ] ; Keep track of difference between the indices ; Traverse the array A [ ] ; Traverse the array B [ ] ; If difference is negative , add N to it ; Keep track of the number of shifts required to place elements at same indices ; Return the max matches ; Driver Code ; Returns the count of matched elements
code: using System ; using System . Collections . Generic ; class GFG { static int maxMatch ( int [ ] A , int [ ] B ) { Dictionary < int , int > Aindex = new Dictionary < int , int > ( ) ; Dictionary < int , int > diff = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < A . Length ; i ++ ) { Aindex [ A [ i ] ] = i ; } for ( int i = 0 ; i < B . Length ; i ++ ) { if ( i - Aindex [ B [ i ] ] < 0 ) { if ( ! diff . ContainsKey ( A . Length + i - Aindex [ B [ i ] ] ) ) { diff [ A . Length + i - Aindex [ B [ i ] ] ] = 1 ; } else { diff [ A . Length + i - Aindex [ B [ i ] ] ] += 1 ; } } else { if ( ! diff . ContainsKey ( i - Aindex [ B [ i ] ] ) ) { diff [ i - Aindex [ B [ i ] ] ] = 1 ; } else { diff [ i - Aindex [ B [ i ] ] ] += 1 ; } } } int max = 0 ; foreach ( KeyValuePair < int , int > ele in diff ) { if ( ele . Value > max ) { max = ele . Value ; } } return max ; } static void Main ( ) { int [ ] A = { 5 , 3 , 7 , 9 , 8 } ; int [ ] B = { 8 , 7 , 3 , 5 , 9 } ; Console . WriteLine ( maxMatch ( A , B ) ) ; } }