XOR of all elements of array with set bits equal to K | C # program to find Xor of all elements 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
using System ; using System . Collections ; using System . Linq ; class GFG { static int xorGivenSetBits ( int [ ] arr , int n , int k ) { ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Convert . ToString ( arr [ i ] , 2 ) . Count ( c => c == '1' ) == k ) { v . Add ( arr [ i ] ) ; } } int result = ( int ) v [ 0 ] ; for ( int i = 1 ; i < v . Count ; i ++ ) result = result ^ ( int ) v [ i ] ; return result ; } static void Main ( ) { int [ ] arr = { 2 , 13 , 1 , 19 , 7 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( xorGivenSetBits ( arr , n , k ) ) ; } }