Count all pairs with given XOR | Java program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty set that stores the visiting element of array . Refer below post for details of unordered_set https : www . geeksforgeeks . org / unorderd_set - stl - uses / ; If there exist an element in set s with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Make element visited ; return total count of pairs with XOR equal to x ; Driver code
import java . util . * ; class GFG { static int xorPairCount ( int arr [ ] , int n , int x ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( x ^ arr [ i ] ) && ( x ^ arr [ i ] ) == ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) { result ++ ; } s . add ( arr [ i ] ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 10 , 15 , 7 , 6 } ; int n = arr . length ; int x = 5 ; System . out . print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " + xorPairCount ( arr , n , x ) ) ; } }