Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Pair of similar elements at different indices in JavaScript
We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.
The function is required to count the number of all such element pairs from the array that are equal in magnitude but are present at different indices.
For example, if the input array is:
const arr = [7, 9, 5, 7, 7, 5];
Then the output should be:
4
because the desired pairs are [7, 7], [7, 7], [7, 7], [5, 5]
How It Works
The algorithm uses a frequency map to track occurrences of each element. For each element, we add its current frequency to the count before incrementing the frequency. This counts all possible pairs with previous occurrences of the same element.
Example
Following is the complete implementation:
const arr = [7, 9, 5, 7, 7, 5];
const equalPairCount = (arr = []) => {
if (!arr?.length) {
return 0;
}
const map = {};
let count = 0;
arr.forEach((val) => {
if (map[val]) {
count += map[val];
}
map[val] = map[val] + 1 || 1;
});
return count;
};
console.log(equalPairCount(arr));
Output
4
Step-by-Step Execution
Let's trace through the algorithm with our example array [7, 9, 5, 7, 7, 5]:
const arr = [7, 9, 5, 7, 7, 5];
const map = {};
let count = 0;
// Step by step execution
arr.forEach((val, index) => {
console.log(`Processing element ${val} at index ${index}`);
console.log(`Current map:`, map);
console.log(`Current count: ${count}`);
if (map[val]) {
count += map[val];
console.log(`Added ${map[val]} pairs, new count: ${count}`);
}
map[val] = map[val] + 1 || 1;
console.log(`Updated frequency of ${val} to ${map[val]}`);
console.log('---');
});
console.log(`Final count: ${count}`);
Output
Processing element 7 at index 0
Current map: {}
Current count: 0
Updated frequency of 7 to 1
---
Processing element 9 at index 1
Current map: { '7': 1 }
Current count: 0
Updated frequency of 9 to 1
---
Processing element 5 at index 2
Current map: { '7': 1, '9': 1 }
Current count: 0
Updated frequency of 5 to 1
---
Processing element 7 at index 3
Current map: { '7': 1, '9': 1, '5': 1 }
Current count: 0
Added 1 pairs, new count: 1
Updated frequency of 7 to 2
---
Processing element 7 at index 4
Current map: { '7': 2, '9': 1, '5': 1 }
Current count: 1
Added 2 pairs, new count: 3
Updated frequency of 7 to 3
---
Processing element 5 at index 5
Current map: { '7': 3, '9': 1, '5': 1 }
Current count: 3
Added 1 pairs, new count: 4
Updated frequency of 5 to 2
---
Final count: 4
Alternative Approach Using Nested Loops
Here's a more intuitive but less efficient O(n²) approach:
const arr = [7, 9, 5, 7, 7, 5];
const equalPairCountBruteForce = (arr = []) => {
let count = 0;
for (let i = 0; i
Output
Found pair: arr[0] = 7, arr[3] = 7
Found pair: arr[0] = 7, arr[4] = 7
Found pair: arr[2] = 5, arr[5] = 5
Found pair: arr[3] = 7, arr[4] = 7
Total pairs: 4
Comparison
| Method | Time Complexity | Space Complexity | Efficiency |
|---|---|---|---|
| Frequency Map | O(n) | O(n) | Optimal |
| Nested Loops | O(n²) | O(1) | Less efficient |
Conclusion
The frequency map approach efficiently counts pairs of equal elements at different indices in O(n) time. It works by tracking how many times each element has been seen and adding that count to the total pairs when encountering the element again.
