Find number of pairs in an array such that their XOR is 0 | Function to calculate the answer ; Finding the maximum of the array ; Creating frequency array With initial value 0 ; Traversing through the array ; Counting frequency ; Traversing through the frequency array ; Calculating answer ; Driver Code ; Function calling
function calculate ( a , n ) { let maximum = Math . max ( ... a ) ; let frequency = new Array ( maximum + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] += 1 ; } let answer = 0 ; for ( let i = 0 ; i < maximum + 1 ; i ++ ) { answer = answer + frequency [ i ] * ( frequency [ i ] - 1 ) ; } return parseInt ( answer / 2 ) ; } let a = [ 1 , 2 , 1 , 2 , 4 ] ; let n = a . length ; document . write ( calculate ( a , n ) ) ;