Given an array **arr[]** of **N **integers, the task for each array element is to find the number of ways of choosing a pair of two equal elements excluding the current element.

**Examples:**

Input:arr[] = {1, 1, 2, 1, 2}Output:2 2 3 2 3Explanation:

For arr[0] (= 1): The remaining array elements are {1, 2, 1, 2}. Possible choice of pairs are (1, 1), (2, 2). Therefore, count is 2.

For arr[1] (= 1): The remaining array elements are {1, 2, 1, 2}. Therefore, count is 2.

For arr[2] (= 2): The remaining array elements are {1, 1, 1, 2}. Possible choice of pairs are (arr[0], arr[1]), (arr[1], arr[2]) and (arr[0], arr[2]). Therefore, count is 3.

For arr[3] (= 1): The remaining elements are {1, 1, 2, 2}. Therefore, count is 2.

For arr[4] (= 2): The remaining elements are {1, 1, 2, 1}. Therefore, count is 3.

Input:arr[] = {1, 2, 1, 4, 2, 1, 4, 1}Output:5 7 5 7 7 5 7 5

**Naive Approach: **The simplest approach to solve this problem is to traverse the array for each array element, count all possible pairs of equal elements from the remaining array.**Time Complexity: **O(N^{3}) **Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can be optimized based on the observation that, for any **i**^{th} index** (1 ≤ i ≤ N), **calculate the following two values:

- The number of ways to choose two distinct elements having equal values from the array.
- The number of ways to choose an element from the
**N − 1**array elements other than the**i**element such that their values are the same as the value of the^{th}**i**element.^{th}

Follow the steps below to solve the problem:

- Initialize a map, say
**mp**, to store the frequency of every array element. - Traverse the map to count the number of pairs made up of equal values. Store the count in a variable, say
**total**. - Traverse the array and for every
**i**^{th}index, print**total – (mp[arr[i]] – 1)**as the required answer.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count the number of` `// required pairs for every array element` `void` `countEqualElementPairs(` `int` `arr[], ` `int` `N)` `{` ` ` `// Initialize a map` ` ` `unordered_map<` `int` `, ` `int` `> mp;` ` ` `// Update the frequency` ` ` `// of every element` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `mp[arr[i]] += 1;` ` ` `}` ` ` `// Stores the count of pairs` ` ` `int` `total = 0;` ` ` `// Traverse the map` ` ` `for` `(` `auto` `i : mp) {` ` ` `// Count the number of ways to` ` ` `// select pairs consisting of` ` ` `// equal elements only` ` ` `total += (i.second * (i.second - 1)) / 2;` ` ` `}` ` ` `// Traverse the array` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// Print the count for` ` ` `// every array element` ` ` `cout << total - (mp[arr[i]] - 1)` ` ` `<< ` `" "` `;` ` ` `}` `}` `// Driver code` `int` `main()` `{` ` ` `// Given array` ` ` `int` `arr[] = { 1, 1, 2, 1, 2 };` ` ` `// Size of the array` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `countEqualElementPairs(arr, N);` `}` |

## Python3

`# Python 3 program for the above approach` `# Function to count the number of` `# required pairs for every array element` `def` `countEqualElementPairs(arr, N):` ` ` ` ` `# Initialize a map` ` ` `mp ` `=` `{}` ` ` `# Update the frequency` ` ` `# of every element` ` ` `for` `i ` `in` `range` `(N):` ` ` `if` `arr[i] ` `in` `mp:` ` ` `mp[arr[i]] ` `+` `=` `1` ` ` `else` `:` ` ` `mp[arr[i]] ` `=` `1` ` ` `# Stores the count of pairs` ` ` `total ` `=` `0` ` ` `# Traverse the map` ` ` `for` `key,value ` `in` `mp.items():` ` ` ` ` `# Count the number of ways to` ` ` `# select pairs consisting of` ` ` `# equal elements only` ` ` `total ` `+` `=` `(value ` `*` `(value ` `-` `1` `)) ` `/` `2` ` ` `# Traverse the array` ` ` `for` `i ` `in` `range` `(N):` ` ` ` ` `# Print the count for` ` ` `# every array element` ` ` `print` `(` `int` `(total ` `-` `(mp[arr[i]] ` `-` `1` `)),end ` `=` `" "` `)` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given array` ` ` `arr ` `=` `[` `1` `, ` `1` `, ` `2` `, ` `1` `, ` `2` `]` ` ` `# Size of the array` ` ` `N ` `=` `len` `(arr)` ` ` `countEqualElementPairs(arr, N)` ` ` ` ` `# This code is contributed by SURENDRA_GANGWAR.` |

**Output:**

2 2 3 2 3

**Time Complexity: **O(N)**Auxiliary Space: **O(N)

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.