Count of index pairs with equal elements in an array | Set 2
Last Updated :
13 Jun, 2022
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, 2, 1, 1}
Output: 3
Explanation:
In the array arr[0]=arr[2]=arr[3]
Valid Pairs are (0, 2), (0, 3) and (2, 3)
Input: arr[]={2, 2, 3, 2, 3}
Output: 4
Explanation:
In the array arr[0]=arr[1]=arr[3] and arr[2]=arr[4]
So Valid Pairs are (0, 1), (0, 3), (1, 3), (2, 4)
For the Naive and Efficient Approach please refer to the previous post of this article.
Better Approach – using Two Pointers: The idea is to sort the given array and the difference of index having the same elements. Below are the steps:
- Sort the given array.
- Initialize the two pointers left and right as 0 and 1 respectively.
- Now till right is less than N, do the following:
- If the element index left and right are the same then increment the right pointer and add the difference of right and left pointer to the final count.
- Else update the value of left to right.
- Print the value of count after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int ans = 0;
sort(arr, arr + n);
int left = 0, right = 1;
while (right < n) {
if (arr[left] == arr[right])
ans += right - left;
else
left = right;
right++;
}
return ans;
}
int main()
{
int arr[] = { 2, 2, 3, 2, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countPairs( int arr[], int n)
{
int ans = 0 ;
Arrays.sort(arr);
int left = 0 , right = 1 ;
while (right < n)
{
if (arr[left] == arr[right])
ans += right - left;
else
left = right;
right++;
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 3 , 2 , 3 };
int N = arr.length;
System.out.print(countPairs(arr, N));
}
}
|
Python3
def countPairs(arr, n):
ans = 0
arr.sort()
left = 0
right = 1 ;
while (right < n):
if (arr[left] = = arr[right]):
ans + = right - left;
else :
left = right;
right + = 1
return ans
if __name__ = = "__main__" :
arr = [ 2 , 2 , 3 , 2 , 3 ]
N = len (arr)
print (countPairs(arr, N))
|
C#
using System;
class GFG{
static int countPairs( int []arr, int n)
{
int ans = 0;
Array.Sort(arr);
int left = 0, right = 1;
while (right < n)
{
if (arr[left] == arr[right])
ans += right - left;
else
left = right;
right++;
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 2, 2, 3, 2, 3 };
int N = arr.Length;
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let ans = 0;
arr.sort( function (a, b){ return a - b});
let left = 0, right = 1;
while (right < n)
{
if (arr[left] == arr[right])
ans += right - left;
else
left = right;
right++;
}
return ans;
}
let arr = [ 2, 2, 3, 2, 3 ];
let N = arr.length;
document.write(countPairs(arr, N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Efficient Approach – using single Traversal: The idea is to use Hashing and update the count of each pair whose frequency is greater than 1. Below are the steps:
- Create a unordered_map M to store the frequency of each element in the array.
- Traverse the given array and keep updating the frequency of each element in M.
- While updating frequency in the above step if the frequency of any element is greater than 0 then count that frequency to the final count.
- Print the count of pairs after the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int ans = 0;
unordered_map< int , int > count;
for ( int i = 0; i < n; i++) {
if (count[arr[i]] != 0)
ans += count[arr[i]];
count[arr[i]]++;
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countPairs(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countPairs( int arr[], int n)
{
int ans = 0 ;
HashMap<Integer,
Integer> count = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (count.containsKey(arr[i]))
{
ans += count.get(arr[i]);
count.put(arr[i], count.get(arr[i]) + 1 );
}
else
{
count.put(arr[i], 1 );
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 1 };
int N = arr.length;
System.out.print(countPairs(arr, N));
}
}
|
Python3
def countPairs(arr, n) :
ans = 0
count = {}
for i in range (n) :
if arr[i] in count :
ans + = count[arr[i]]
if arr[i] in count :
count[arr[i]] + = 1
else :
count[arr[i]] = 1
return ans
arr = [ 1 , 2 , 1 , 1 ]
N = len (arr)
print (countPairs(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countPairs( int []arr, int n)
{
int ans = 0;
Dictionary< int ,
int > count = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (count.ContainsKey(arr[i]))
{
ans += count[arr[i]];
count[arr[i]] = count[arr[i]] + 1;
}
else
{
count.Add(arr[i], 1);
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 1, 1 };
int N = arr.Length;
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let ans = 0;
let count = new Map();
for (let i = 0; i < n; i++)
{
if (count.has(arr[i]))
{
ans += count.get(arr[i]);
count.set(arr[i], count.get(arr[i]) + 1);
}
else
{
count.set(arr[i], 1);
}
}
return ans;
}
let arr = [ 1, 2, 1, 1 ];
let N = arr.length;
document.write(countPairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...