text: Count elements of same value placed at same indices of two given arrays | 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: function maxMatch ( A , B ) { var Aindex = { } ; var diff = { } ; for ( var i = 0 ; i < A . length ; i ++ ) { Aindex [ A [ i ] ] = i ; } for ( var i = 0 ; i < B . length ; i ++ ) { if ( i - Aindex [ B [ i ] ] < 0 ) { if ( ! diff . hasOwnProperty ( 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 . hasOwnProperty ( i - Aindex [ B [ i ] ] ) ) { diff [ i - Aindex [ B [ i ] ] ] = 1 ; } else { diff [ i - Aindex [ B [ i ] ] ] += 1 ; } } } var max = 0 ; for ( const [ key , value ] of Object . entries ( diff ) ) { if ( value > max ) { max = value ; } } return max ; } var A = [ 5 , 3 , 7 , 9 , 8 ] ; var B = [ 8 , 7 , 3 , 5 , 9 ] ; document . write ( maxMatch ( A , B ) ) ;