Unique element in an array where all elements occur K times except one | Set 2 | Function that find the unique element in the array arr [ ] ; Store all unique element in set ; Sum of all element of the array ; Sum of element in the set ; Print the unique element using formula ; Driver Code ; Function call
function findUniqueElements ( arr , N , K ) { var s = new Set ( arr ) ; var arr_sum = arr . reduce ( ( a , b ) => a + b , 0 ) ; var set_sum = 0 ; s . forEach ( function ( value ) { set_sum += value ; } ) document . write ( Math . floor ( ( K * set_sum - arr_sum ) / ( K - 1 ) ) ) ; } var arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ] ; var N = arr . length ; var K = 3 ; findUniqueElements ( arr , N , K ) ;