Count of index pairs with equal elements in an array

Given an array 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 : 1
As arr[0] = arr[1], the pair of indices is (0, 1)

Input : arr[] = {1, 1, 1}
Output : 3
As arr[0] = arr[1], the pair of indices is (0, 1), 
(0, 2) and (1, 2)

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

Method 1 (Brute Force):
For each index i, find element after it with same value as arr[i]. Below is C++ implementation of this approach:

// C++ program to count of pairs with equal
// elements in an array.
#include<bits/stdc++.h>
using namespace std;

// Return the number of pairs with equal
// values.
int countPairs(int arr[], int n)
{
    int ans = 0;

    // for each index i and j
    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)

            // finding the index with same
            // value but different index.
            if (arr[i] == arr[j])
                ans++;
    return ans;
}

// Driven Program
int main()
{
    int arr[] = { 1, 1, 2 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << countPairs(arr, n) << endl;
    return 0;
}

Output :

1

Time Complexity : O(n2)

 

Method 2 (Efficient approach):
The idea is to count the frequency of each number and then find the number of pairs with equal elements. Suppose, a number x appears k times at index i1, i2,….,ik. Then pick any two indexes ix and iy which will be counted as 1 pair. Similarly, iy and ix can also be pair. So, choose nC2 is the number of pairs such that arr[i] = arr[j] = x.

Below is C++ implementation of this approach:

// C++ program to count of index pairs with
// equal elements in an array.
#include<bits/stdc++.h>
using namespace std;

// Return the number of pairs with equal
// values.
int countPairs(int arr[], int n)
{
    unordered_map<int, int> mp;

    // Finding frequency of each number.
    for (int i = 0; i < n; i++)
        mp[arr[i]]++;

    // Calculating pairs of each value.
    int ans = 0;
    for (auto it=mp.begin(); it!=mp.end(); it++)
    {
        int count = it->second;
        ans += (count * (count - 1))/2;
    }

    return ans;
}

// Driven Program
int main()
{
    int arr[] = {1, 1, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << countPairs(arr, n) << endl;
    return 0;
}

Output:

1

Time Complexity : O(n)

Source:
http://stackoverflow.com/questions/26772364/efficient-algorithm-for-counting-number-of-pairs-of-identical-elements-in-an-arr#comment42124861_26772516

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.