JavaScript Program for Number of unique triplets whose XOR is zero

JavaScript Program for the Number of unique triplets whose XOR is zero is a common programming problem that requires finding the number of unique triplets in a given array whose XOR is equal to zero. The XOR operation is a bitwise operator that takes two bits and returns 1 if the bits are different, and 0 if they are the same. In this problem, we need to find all possible combinations of three numbers in the array, where the XOR of the triplet is zero.

Problem Statement

We are required to find the number of unique triplets in a given array whose XOR is equal to zero. In other words, we need to find all possible combinations of three numbers in the array, where the XOR of the triplet is zero.

Understanding XOR Property

The key insight is that for three numbers a, b, and c: if a ? b ? c = 0, then a ? b = c. This means if we know two numbers, we can determine the third number that makes their XOR equal to zero.

Sample Examples

Example 1:

Input: arr = [4, 7, 5, 8, 3, 9]
Output: 1

Explanation: There is only one unique triplet with XOR equal to zero: [4, 7, 3] because 4 ? 7 ? 3 = 0.

Example 2:

Input: arr = [1, 3, 5, 10, 14, 15]
Output: 2

Explanation: There are two unique triplets: [1, 14, 15] and [5, 10, 15].

Approach Comparison

Method Time Complexity Space Complexity Suitable for
Brute Force O(n³) O(1) Small arrays
Sorting + Two Pointer O(n²logn) O(1) Medium arrays
Hashing O(n²) O(n) Large arrays

Hashing Algorithm

The hashing approach uses the XOR property efficiently:

  1. Create a Set from the array for O(1) lookup
  2. For each pair (i, j) where i < j, calculate XOR
  3. Check if the XOR result exists in the set
  4. Ensure the third element is different from the first two
  5. Divide final count by 3 to avoid counting duplicates

Implementation

function countTriplets(arr) {
    const n = arr.length;
    
    // Create set for O(1) lookup
    const set = new Set(arr);
    
    let count = 0;
    
    // Check all pairs (i, j) where i < j
    for (let i = 0; i < n; i++) {
        for (let j = i + 1; j < n; j++) {
            // Calculate XOR of arr[i] and arr[j]
            const xor = arr[i] ^ arr[j];
            
            // Check if third element exists and is different
            if (set.has(xor) && xor !== arr[i] && xor !== arr[j]) {
                count++;
            }
        }
    }
    
    // Each triplet is counted 3 times, so divide by 3
    return Math.floor(count / 3);
}

// Test with examples
console.log("Example 1:");
const arr1 = [4, 7, 5, 8, 3, 9];
console.log("Input:", arr1);
console.log("Output:", countTriplets(arr1));

console.log("\nExample 2:");
const arr2 = [1, 3, 5, 10, 14, 15];
console.log("Input:", arr2);
console.log("Output:", countTriplets(arr2));
Example 1:
Input: [ 4, 7, 5, 8, 3, 9 ]
Output: 1

Example 2:
Input: [ 1, 3, 5, 10, 14, 15 ]
Output: 2

How It Works

The algorithm works by leveraging the mathematical property that if a ? b ? c = 0, then c = a ? b. For each pair of elements, we calculate their XOR and check if that result exists in the array as a third element. The count is divided by 3 because each triplet gets counted three times (once for each possible pairing).

Time and Space Complexity

  • Time Complexity: O(n²) - nested loops through the array
  • Space Complexity: O(n) - for the Set data structure

Conclusion

The hashing approach provides an efficient O(n²) solution to count unique triplets with XOR equal to zero. This technique is particularly useful for cryptographic applications and demonstrates the power of combining mathematical properties with efficient data structures.

Updated on: 2026-03-15T23:19:01+05:30

480 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements