Find number of pairs in an array such that their XOR is 0 | Java program to find number of pairs in an array such that their XOR is 0 ; Function to calculate the count ; Sorting the list using built in function ; Traversing through the elements ; Counting frequency of each elements ; Adding the contribution of the frequency to the answer ; Driver Code ; Print the count
import java . util . * ; class GFG { static int calculate ( int a [ ] , int n ) { Arrays . sort ( a ) ; int count = 1 ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { count += 1 ; } else { answer = answer + ( count * ( count - 1 ) ) / 2 ; count = 1 ; } } answer = answer + ( count * ( count - 1 ) ) / 2 ; return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 4 } ; int n = a . length ; System . out . println ( calculate ( a , n ) ) ; } }