Related Articles

# Find number of pairs in an array such that their XOR is 0

• Difficulty Level : Easy
• Last Updated : 20 Jul, 2021

Given an array of size N. Find the number of pairs (i, j) such that XOR = 0, and 1 <= i < j <= N.
Examples :

```Input : A[] = {1, 3, 4, 1, 4}
Output : 2
Explanation : Index (0, 3) and (2, 4)

Input : A[] = {2, 2, 2}
Output : 3```

First Approach : Sorting XOR = 0 is only satisfied when . Therefore, we will first sort the array and then count the frequency of each element. By combinatorics, we can observe that if frequency of some element is then, it will contribute to the answer.
Below is the implementation of above approach:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

## C++

 `// C++ program to find number``// of pairs in an array such``// that their XOR is 0``#include ``using` `namespace` `std;` `// Function to calculate the``// count``int` `calculate(``int` `a[], ``int` `n)``{``    ``// Sorting the list using``    ``// built in function``    ``sort(a, a + n);` `    ``int` `count = 1;``    ``int` `answer = 0;` `    ``// Traversing through the``    ``// elements``    ``for` `(``int` `i = 1; i < n; i++) {``    ` `        ``if` `(a[i] == a[i - 1]){` `            ``// Counting frequency of each``            ``// elements``            ``count += 1;``            ` `        ``}``        ``else``        ``{``            ``// Adding the contribution of``            ``// the frequency to the answer``            ``answer = answer + (count * (count - 1)) / 2;``            ``count = 1;``        ``}``    ``}` `    ``answer = answer + (count * (count - 1)) / 2;` `    ``return` `answer;``}` `// Driver Code``int` `main()``{` `    ``int` `a[] = { 1, 2, 1, 2, 4 };``    ``int` `n = ``sizeof``(a) / ``sizeof``(a);` `    ``// Print the count``    ``cout << calculate(a, n);``    ``return` `0;``}` `// This article is contributed by Sahil_Bansall.`

## Java

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

## Python3

 `# Python3 program to find number of pairs``# in an array such that their XOR is 0` `# Function to calculate the count``def` `calculate(a) :` `    ``# Sorting the list using``    ``# built in function``    ``a.sort()``    ` `    ``count ``=` `1``    ``answer ``=` `0``    ` `    ``# Traversing through the elements``    ``for` `i ``in` `range``(``1``, ``len``(a)) :` `        ``if` `a[i] ``=``=` `a[i ``-` `1``] :``            ` `            ``# Counting frequency of each elements``            ``count ``+``=` `1` `        ``else` `:` `            ``# Adding the contribution of``            ``# the frequency to the answer``            ``answer ``=` `answer ``+` `count ``*` `(count ``-` `1``) ``/``/` `2``            ``count ``=` `1` `    ``answer ``=` `answer ``+` `count ``*` `(count ``-` `1``) ``/``/` `2``    ` `    ``return` `answer`  `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``a ``=` `[``1``, ``2``, ``1``, ``2``, ``4``]` `    ``# Print the count``    ``print``(calculate(a))`

## C#

 `// C# program to find number``// of pairs in an array such``// that their XOR is 0``using` `System;` `class` `GFG``{``    ``// Function to calculate``    ``// the count``    ``static` `int` `calculate(``int` `[]a, ``int` `n)``    ``{``        ``// Sorting the list using``        ``// built in function``        ``Array.Sort(a);``    ` `        ``int` `count = 1;``        ``int` `answer = 0;``    ` `        ``// Traversing through the``        ``// elements``        ``for` `(``int` `i = 1; i < n; i++)``        ``{``        ` `            ``if` `(a[i] == a[i - 1])``            ``{``                ``// Counting frequency of each``                ``// elements``                ``count += 1;``                ` `            ``}``            ``else``            ``{``                ``// Adding the contribution of``                ``// the frequency to the answer``                ``answer = answer + (count * (count - 1)) / 2;``                ``count = 1;``            ``}``        ``}``    ` `        ``answer = answer + (count * (count - 1)) / 2;``    ` `        ``return` `answer;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main ()``    ``{``        ``int` `[]a = { 1, 2, 1, 2, 4 };``        ``int` `n = a.Length;``    ` `        ``// Print the count``        ``Console.WriteLine(calculate(a, n));``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ``

## Javascript

 ``

Output :

`2`

Time Complexity : O(N Log N)
Second Approach : Hashing (Index Mapping)
Solution is handy, if we can count the frequency of each element in the array. Index mapping technique can be used to count the frequency of each element.
Below is the implementation of above approach :

## C++

 `// C++ program to find number of pairs``// in an array such that their XOR is 0``#include ``using` `namespace` `std;` `// Function to calculate the answer``int` `calculate(``int` `a[], ``int` `n){``    ` `    ``// Finding the maximum of the array``    ``int` `*maximum = max_element(a, a + n);` `    ``// Creating frequency array``    ``// With initial value 0``    ``int` `frequency[*maximum + 1] = {0};``    ` `    ``// Traversing through the array``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``// Counting frequency``        ``frequency[a[i]] += 1;``    ``}``    ``int` `answer = 0;``    ` `    ``// Traversing through the frequency array``    ``for``(``int` `i = 0; i < (*maximum)+1; i++)``    ``{``        ``// Calculating answer``        ``answer = answer + frequency[i] * (frequency[i] - 1) ;``    ``}``    ``return` `answer/2;``}` `// Driver Code``int` `main()``{``   ``int` `a[] = {1, 2, 1, 2, 4};``   ``int` `n = ``sizeof``(a) / ``sizeof``(a);``   ` `   ``// Function calling``   ``cout << (calculate(a,n));``}` `// This code is contributed by Smitha`

## Java

 `// Java program to find number of pairs``// in an array such that their XOR is 0``import` `java.util.*;` `class` `GFG``{` `    ``// Function to calculate the answer``    ``static` `int` `calculate(``int` `a[], ``int` `n)``    ``{` `        ``// Finding the maximum of the array``        ``int` `maximum = Arrays.stream(a).max().getAsInt();` `        ``// Creating frequency array``        ``// With initial value 0``        ``int` `frequency[] = ``new` `int``[maximum + ``1``];` `        ``// Traversing through the array``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ` `            ``// Counting frequency``            ``frequency[a[i]] += ``1``;``        ``}``        ``int` `answer = ``0``;` `        ``// Traversing through the frequency array``        ``for` `(``int` `i = ``0``; i < (maximum) + ``1``; i++)``        ``{``            ` `            ``// Calculating answer``            ``answer = answer + frequency[i] * (frequency[i] - ``1``);``        ``}``        ``return` `answer / ``2``;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `a[] = {``1``, ``2``, ``1``, ``2``, ``4``};``        ``int` `n = a.length;` `        ``// Function calling``        ``System.out.println(calculate(a, n));``    ``}``}` `// This code is contributed by 29AjayKumar`

## Python 3

 `# Python3 program to find number of pairs``# in an array such that their XOR is 0` `# Function to calculate the answer``def` `calculate(a) :``    ` `    ``# Finding the maximum of the array``    ``maximum ``=` `max``(a)``    ` `    ``# Creating frequency array``    ``# With initial value 0``    ``frequency ``=` `[``0` `for` `x ``in` `range``(maximum ``+` `1``)]``    ` `    ``# Traversing through the array``    ``for` `i ``in` `a :``         ` `        ``# Counting frequency``        ``frequency[i] ``+``=` `1``    ` `    ``answer ``=` `0``    ` `    ``# Traversing through the frequency array``    ``for` `i ``in` `frequency :``        ` `        ``# Calculating answer``        ``answer ``=` `answer ``+` `i ``*` `(i ``-` `1``) ``/``/` `2``    ` `    ``return` `answer` `# Driver Code``a ``=` `[``1``, ``2``, ``1``, ``2``, ``4``]``print``(calculate(a))`

## C#

 `// C# program to find number of pairs``// in an array such that their XOR is 0``using` `System;``using` `System.Linq;``class` `GFG``{` `    ``// Function to calculate the answer``    ``static` `int` `calculate(``int` `[]a, ``int` `n)``    ``{` `        ``// Finding the maximum of the array``        ``int` `maximum = a.Max();` `        ``// Creating frequency array``        ``// With initial value 0``        ``int` `[]frequency = ``new` `int``[maximum + 1];` `        ``// Traversing through the array``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ` `            ``// Counting frequency``            ``frequency[a[i]] += 1;``        ``}``        ``int` `answer = 0;` `        ``// Traversing through the frequency array``        ``for` `(``int` `i = 0; i < (maximum) + 1; i++)``        ``{``            ` `            ``// Calculating answer``            ``answer = answer + frequency[i] *``                             ``(frequency[i] - 1);``        ``}``        ``return` `answer / 2;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `[]a = {1, 2, 1, 2, 4};``        ``int` `n = a.Length;` `        ``// Function calling``        ``Console.WriteLine(calculate(a, n));``    ``}``}` `// This code is contributed by PrinciRaj1992`

## PHP

 ``

## Javascript

 ``

Output :

`2`

Time Complexity : O(N)
Note : Index Mapping method can only be used when the numbers in the array are not large. In such cases, sorting method can be used.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6415133,"math_prob":0.9842469,"size":10945,"snap":"2021-43-2021-49","text_gpt3_token_len":3489,"char_repetition_ratio":0.17804588,"word_repetition_ratio":0.55239785,"special_character_ratio":0.3649155,"punctuation_ratio":0.15092503,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999136,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"im_url_duplicate_count":[null,1,null,2,null,2,null,2,null,2,null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-17T07:18:55Z\",\"WARC-Record-ID\":\"<urn:uuid:826aa616-1df4-4b5d-a84b-ff2d6dd66940>\",\"Content-Length\":\"221959\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:64ccc687-f90e-45fb-a088-6062219a16ed>\",\"WARC-Concurrent-To\":\"<urn:uuid:42f38b76-75c6-49d0-b2a6-6cb8a3733a00>\",\"WARC-IP-Address\":\"23.207.202.207\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/find-number-pairs-array-xor-0/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:SP2VZVI5WR2BNOBNMO5MCVWL4XFYUFLE\",\"WARC-Block-Digest\":\"sha1:TQMBNEZUQGVCWF26YTF74D22B4VUG7RZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585121.30_warc_CC-MAIN-20211017052025-20211017082025-00307.warc.gz\"}"}