Find the repeating element in an Array of size N consisting of first M natural numbers | Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Input ; Function call
function repeatingElement ( arr , N ) { let M = 0 , sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = Math . max ( M , arr [ i ] ) ; } let sum1 = parseInt ( M * ( M + 1 ) / 2 ) ; let ans = parseInt ( ( sum - sum1 ) / ( N - M ) ) ; return ans ; } let arr = [ 2 , 6 , 4 , 3 , 1 , 5 , 2 ] ; let N = arr . length ; document . write ( repeatingElement ( arr , N ) ) ;