JavaScript Program to Count Unequal Element Pairs from the Given Array
Last Updated :
29 Aug, 2024
Unequal element pairs in an array refer to pairs of distinct elements within the array that have different values. These pairs consist of two elements that are not equal to each other, highlighting their inequality when compared in the context of the array's values.
Examples:
Input: arr[] = {6, 5, 2, 3, 5, 2, 2, 1}
Output: 2
Explanation: (arr[1], arr[4]) and (arr[2], arr[5]) are the only possible pairs.
Input: arr[] = {1, 2, 3, 1, 2}
Output: 2
Using Sort() method
In this approach, the function countUnequalPairs sorts the input array, and then iterates through unique elements. For each element, it compares with the rest of the array, counting unequal pairs. The result represents the count of unequal pairs.
Syntax
array.sort()
Example: This example shows the use of the above-explained approach.
JavaScript
const countUnequalPairs = (
inputArray) => {
inputArray.sort((a, b) => a - b);
let unEqualPairsCount = 0;
for (
let i = 0;
i < inputArray.length;) {
let currentElement =
inputArray[i];
let j = i + 1;
for (let num of inputArray.slice(j)) {
if (
num !== currentElement) {
unEqualPairsCount++;
}
}
i = j;}
return unEqualPairsCount;
};
const testArray = [1, 1, 2];
console.log(
countUnequalPairs(testArray)
);
Using Hash Map
In this approach, we use a hash map (or an object in JavaScript) to count the occurrences of each element in the array. By leveraging the counts of each element, we can determine the number of unequal pairs efficiently.
JavaScript
function countUnequalPairs(arr) {
const frequency = {};
let totalPairs = 0;
let unequalPairs = 0;
// Step 1: Build frequency object
for (const element of arr) {
frequency[element] = (frequency[element] || 0) + 1;
}
// Step 2: Calculate total pairs
for (const key in frequency) {
totalPairs += frequency[key];
}
// Step 3: Calculate unequal pairs
for (const key in frequency) {
const count = frequency[key];
unequalPairs += count * (totalPairs - count);
}
// Since each pair is counted twice, divide the result by 2
return unequalPairs / 2;
}
// Example usage:
const arr1 = [6, 5, 2, 3, 5, 2, 2, 1];
console.log(countUnequalPairs(arr1)); // Output: 24
const arr2 = [1, 2, 3, 1, 2];
console.log(countUnequalPairs(arr2)); // Output: 8
Using a Set to Count Unique Elements and Calculate Pairs
In this approach, we use a Set to count the occurrences of each unique element in the array. By leveraging the counts of each unique element, we can determine the number of unequal pairs efficiently without needing nested loops or sorting.
Example:
JavaScript
function countUnequalPairs(arr) {
const elementCount = new Map();
let totalPairs = 0;
let totalElements = arr.length;
arr.forEach(num => {
if (elementCount.has(num)) {
elementCount.set(num, elementCount.get(num) + 1);
} else {
elementCount.set(num, 1);
}
});
elementCount.forEach(count => {
totalPairs += count * (totalElements - count);
totalElements -= count;
});
return totalPairs;
}
let arr1 = [6, 5, 2, 3, 5, 2, 2, 1];
console.log(countUnequalPairs(arr1));
let arr2 = [1, 2, 3, 1, 2];
console.log(countUnequalPairs(arr2));
Using Single-Pass Approach (Optimized)
Use an efficient method to count unequal pairs in a single pass through the array, avoiding nested loops. This method Optimized for large arrays, avoiding nested loops for efficient performance.
Example:
JavaScript
function countUnequalPairs(arr) {
const count = new Map();
let totalPairs = 0;
let totalElements = 0;
for (const num of arr) {
if (count.has(num)) {
totalPairs += (totalElements - count.get(num));
count.set(num, count.get(num) + 1);
} else {
count.set(num, 1);
totalPairs += totalElements;
}
totalElements++;
}
return totalPairs;
}
let arr1 = [6, 5, 2, 3, 5, 2, 2, 1];
console.log(countUnequalPairs(arr1));
let arr2 = [1, 2, 3, 1, 2];
console.log(countUnequalPairs(arr2));
Combinatorial Approach with Basic Array Operations
- Calculate Total Pairs: Calculate the total number of possible pairs in the array using combinatorics.
- Count Equal Pairs: Iterate through the array to count pairs where both elements are equal.
- Compute Unequal Pairs: Subtract the count of equal pairs from the total pairs to get the number of unequal pairs.
Steps:
- Calculate Total Pairs: Use the formula n(n−1)/2​, where n is the length of the array, to find the total number of pairs.
- Count Equal Pairs: Use nested loops to count the number of pairs with equal elements.
- Subtract Equal Pairs: Subtract the count of equal pairs from the total pairs to get the number of unequal pairs.
Example:
JavaScript
function countUnequalPairs(arr) {
const n = arr.length;
let totalPairs = 0;
let equalPairs = 0;
// Calculate total number of pairs
totalPairs = (n * (n - 1)) / 2;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (arr[i] === arr[j]) {
equalPairs++;
}
}
}
const unequalPairs = totalPairs - equalPairs;
return unequalPairs;
}
const arr1 = [6, 5, 2, 3, 5, 2, 2, 1];
const arr2 = [1, 2, 3, 1, 2];
console.log(countUnequalPairs(arr1));
console.log(countUnequalPairs(arr2));
Similar Reads
JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <=
3 min read
Count pairs in given Array having sum of index and value at that index equal Given an array arr[] containing positive integers, count the total number of pairs for which arr[i]+i = arr[j]+j such that 0â¤i<jâ¤n-1. Examples: Input: arr[] = { 6, 1, 4, 3 }Output: 3Explanation: The elements at index 0, 2, 3 has same value of a[i]+i as all sum to 6 {(6+0), (4+2), (3+3)}. Input: a
8 min read
Count same Value Pairs with Minimum Separation Given an array a[] of size n and integer k. The task is to count a number of different pairs of indexes where values at a given index are the same and both indexes are at least k indices apart. Examples: Input: n = 5, k = 2 a = {1, 2, 2, 1, 2} Output: 3Explanation: Possible pairs are found in indice
6 min read
Maximum number of Valid Pairs satisfying conditions k*(j - i) != (arr[j] - arr[i])/k Given an array arr[] of size N and an integer k, the task is to find the maximum number of valid pair of indices (i, j) such that i < j and k*(j - i) != (arr[j] - arr[i])/k. Example: Input: arr[] = {4, 1, 5, 3}, k = 2Output: 5Explanation: All possible pair is valid except {arr[1] , arr[2]}, becau
7 min read
Count of pairs (arr[i], arr[j]) such that arr[i] + j and arr[j] + i are equal Given an array arr[], the task is to count pairs i, j such that, i < j and arr[i] + j = arr[j] + i. Examples: Input: arr[] = {4, 1, 2, 3}Output: 3Explanation: In total three pairs are satisfying the given condition those are {1, 2}, {2, 3} and {1, 3}.So, the final answer is 3. Input: arr[] = {1,
5 min read
Count pairs such that difference between them and indices are different Given an array arr[] of size N, the task is to count all pair of indices (i, j) such that i < j and j - i != arr[j] - arr[i]. Examples: Input: arr[] = {4, 1, 3, 3}Output: 5Explanation:The pair (0, 1) is a valid pair since 1 - 0 != 1 - 4.The pair (0, 2) is a valid pair since 2 - 0 != 3 - 4, 2 != -
7 min read
Count unequal element pairs from the given Array Given an array arr[] of N elements. The task is to count the total number of indices (i, j) such that arr[i] != arr[j] and i < j.Examples: Input: arr[] = {1, 1, 2} Output: 2 (1, 2) and (1, 2) are the only valid pairs.Input: arr[] = {1, 2, 3} Output: 3Input: arr[] = {1, 1, 1} Output: 0 Recommended
10 min read
Count all distinct pairs of repeating elements from the array for every array element Given an array arr[] of N integers. For each element in the array, the task is to count the possible pairs (i, j), excluding the current element, such that i < j and arr[i] = arr[j]. Examples: Input: arr[] = {1, 1, 2, 1, 2}Output: 2 2 3 2 3Explanation:For index 1, remaining elements after excludi
7 min read
Pairs with Difference less than K Given an array of n integers, We need to find all pairs with a difference less than k Examples : Input : a[] = {1, 10, 4, 2} K = 3 Output : 2 We can make only two pairs with difference less than 3. (1, 2) and (4, 2) Input : a[] = {1, 8, 7} K = 7 Output : 2 Pairs with difference less than 7 are (1, 7
13 min read
Count of pairs (x, y) in an array such that x < y Given an array of N distinct integers, the task is to find the number of pairs (x, y) such that x < y. Example: Input: arr[] = {2, 4, 3, 1} Output: 6 Possible pairs are (1, 2), (1, 3), (1, 4), (2, 3), (2, 4) and (3, 4).Input: arr[] = {5, 10} Output: 1 The only possible pair is (5, 10). Naive appr
7 min read