XOR of all elements of array with set bits equal to K | Function to find Xor of desired elements ; Initialize vector ; push required elements ; Initialize result with first element of vector ; Driver code
function xorGivenSetBits ( arr , n , k ) { let v = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( bitcount ( arr [ i ] ) == k ) { v . push ( arr [ i ] ) ; } } let result = v [ 0 ] ; for ( let i = 1 ; i < v . length ; i ++ ) result = result ^ v [ i ] ; return result ; } function bitcount ( n ) { var count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } let arr = [ 2 , 13 , 1 , 19 , 7 ] ; let n = arr . length ; let k = 3 ; document . write ( xorGivenSetBits ( arr , n , k ) ) ;