Count of index pairs with equal elements in an array
AuxilGiven 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 the implementation of this approach:
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i+1; j < n; j++)
if (arr[i] == arr[j])
ans++;
return ans;
}
int main()
{
int arr[] = { 1, 1, 2 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << countPairs(arr, n) << endl;
return 0;
}
|
Java
class GFG {
static int countPairs( int arr[], int n)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = i+ 1 ; j < n; j++)
if (arr[i] == arr[j])
ans++;
return ans;
}
public static void main (String[] args)
{
int arr[] = { 1 , 1 , 2 };
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
|
Python3
def countPairs(arr, n):
ans = 0
for i in range ( 0 , n):
for j in range (i + 1 , n):
if (arr[i] = = arr[j]):
ans + = 1
return ans
arr = [ 1 , 1 , 2 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
class GFG {
static int countPairs( int []arr, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
for ( int j = i+1; j < n; j++)
if (arr[i] == arr[j])
ans++;
return ans;
}
public static void Main ()
{
int []arr = { 1, 1, 2 };
int n = arr.Length;
Console.WriteLine(countPairs(arr, n));
}
}
|
PHP
<?php
function countPairs( $arr , $n )
{
$ans = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $i + 1; $j < $n ; $j ++)
if ( $arr [ $i ] == $arr [ $j ])
$ans ++;
return $ans ;
}
$arr = array ( 1, 1, 2 );
$n = count ( $arr );
echo countPairs( $arr , $n ) ;
?>
|
Javascript
<script>
function countPairs(arr, n)
{
let ans = 0;
for (let i = 0; i < n; i++)
for (let j = i+1; j < n; j++)
if (arr[i] == arr[j])
ans++;
return ans;
}
let arr = [ 1, 1, 2 ];
let n = arr.length;
document.write(countPairs(arr, n));
</script>
|
Time Complexity : O(n2)
Auxiliary Space: O(1)
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 the implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
int countPairs( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
int ans = 0;
for ( auto it=mp.begin(); it!=mp.end(); it++)
{
int count = it->second;
ans += (count * (count - 1))/2;
}
return ans;
}
int main()
{
int arr[] = {1, 1, 2};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << countPairs(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int countPairs( int arr[], int n)
{
HashMap<Integer,Integer> hm = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (hm.containsKey(arr[i]))
hm.put(arr[i],hm.get(arr[i]) + 1 );
else
hm.put(arr[i], 1 );
}
int ans= 0 ;
for (Map.Entry<Integer,Integer> it : hm.entrySet())
{
int count = it.getValue();
ans += (count * (count - 1 )) / 2 ;
}
return ans;
}
public static void main(String[] args)
{
int arr[] = new int []{ 1 , 2 , 3 , 1 };
System.out.println(countPairs(arr,arr.length));
}
}
|
Python3
import math as mt
def countPairs(arr, n):
mp = dict ()
for i in range (n):
if arr[i] in mp.keys():
mp[arr[i]] + = 1
else :
mp[arr[i]] = 1
ans = 0
for it in mp:
count = mp[it]
ans + = (count * (count - 1 )) / / 2
return ans
arr = [ 1 , 1 , 2 ]
n = len (arr)
print (countPairs(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int countPairs( int []arr, int n)
{
Dictionary< int ,
int > hm = new Dictionary< int ,
int >();
for ( int i = 0; i < n; i++)
{
if (hm.ContainsKey(arr[i]))
{
int a = hm[arr[i]];
hm.Remove(arr[i]);
hm.Add(arr[i], a + 1);
}
else
hm.Add(arr[i], 1);
}
int ans = 0;
foreach ( var it in hm)
{
int count = it.Value;
ans += (count * (count - 1)) / 2;
}
return ans;
}
public static void Main()
{
int []arr = new int []{1, 2, 3, 1};
Console.WriteLine(countPairs(arr,arr.Length));
}
}
|
Javascript
<script>
function countPairs(arr,n)
{
let hm = new Map();
for (let i = 0; i < n; i++)
{
if (hm.has(arr[i]))
hm.set(arr[i],hm.get(arr[i]) + 1);
else
hm.set(arr[i], 1);
}
let ans=0;
for (let [key, value] of hm.entries())
{
let count = value;
ans += (count * (count - 1)) / 2;
}
return ans;
}
let arr=[1, 2, 3, 1];
document.write(countPairs(arr,arr.length));
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(n)
Last Updated :
12 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...