Find the two repeating elements in a given array | printRepeating function ; S is for sum of elements in arr ; P is for product of elements in arr ; x and y are two repeating elements ; D is for difference of x and y , i . e . , x - y ; Calculate Sum and Product of all elements in arr ; S is x + y now ; P is x * y now ; D is x - y now ; factorial of n ; driver code
function printRepeating ( arr , size ) { var S = 0 ; var P = 1 ; var x , y ; var D ; var n = size - 2 , i ; for ( i = 0 ; i < size ; i ++ ) { S = S + arr [ i ] ; P = P * arr [ i ] ; } S = S - n * parseInt ( ( n + 1 ) / 2 ) ; P = parseInt ( P / fact ( n ) ) ; D = parseInt ( Math . sqrt ( S * S - 4 * P ) ) ; x = parseInt ( ( D + S ) / 2 ) ; y = parseInt ( ( S - D ) / 2 ) ; document . write ( " " ) ; document . write ( x + " " + y ) ; } function fact ( n ) { var ans = false ; if ( n == 0 ) return 1 ; else return ( n * fact ( n - 1 ) ) ; } var arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] ; var arr_size = arr . length ; printRepeating ( arr , arr_size ) ;