Check if the array has an element which is equal to XOR of remaining elements | Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver code
function containsElement ( arr , n ) { let xorArr = 0 ; for ( let i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( let i = 0 ; i < n ; ++ i ) { let x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } let arr = [ 8 , 2 , 4 , 15 , 1 ] ; let n = arr . length ; if ( containsElement ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;