Count equal element pairs in the given array
Last Updated :
12 Jul, 2025
Given an array arr[] of N integers representing the lengths of the gloves, the task is to count the maximum possible pairs of gloves from the given array. Note that a glove can only pair with a same-sized glove and it can only be part of a single pair.
Examples:
Input: arr[] = {6, 5, 2, 3, 5, 2, 2, 1}
Output: 2
Explanation: (arr[1], arr[4]) and (arr[2], arr[5]) are the only possible pairs.
Input: arr[] = {1, 2, 3, 1, 2}
Output: 2
Simple Approach: Sort the given array so that all the equal elements are adjacent to each other. Now, traverse the array and for every element, if it is equal to the element next to it then it is a valid pair and skips these two elements. Else the current element doesn't make a valid pair with any other element and hence only skips the current element.
Below is the implementation of the above approach:
C++14
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum
// possible pairs of gloves
int cntgloves(int arr[], int n)
{
// To store the required count
int count = 0;
// Sort the original array
sort(arr, arr + n);
for (int i = 0; i < n - 1;) {
// A valid pair is found
if (arr[i] == arr[i + 1]) {
count++;
// Skip the elements of
// the current pair
i = i + 2;
}
// Current elements doesn't make
// a valid pair with any other element
else {
i++;
}
}
return count;
}
// Driver code
int main()
{
int arr[] = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << cntgloves(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG {
// Function to return the maximum
// possible pairs of gloves
static int cntgloves(int arr[], int n)
{
// Sort the original array
Arrays.sort(arr);
int res = 0;
int i = 0;
while (i < n) {
// take first number
int number = arr[i];
int count = 1;
i++;
// Count all duplicates
while (i < n && arr[i] == number) {
count++;
i++;
}
// If we spotted number just 2
// times, increment
// result
if (count >= 2) {
res = res + count / 2;
}
}
return res;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {6, 5, 2, 3, 5, 2, 2, 1};
int n = arr.length;
// Function call
System.out.println(cntgloves(arr, n));
}
}
// This code is contributed by Lakhan murmu
Python3
# Python3 implementation of the approach
# Function to return the maximum
# possible pairs of gloves
def cntgloves(arr, n):
# To store the required count
count = 0
# Sort the original array
arr.sort()
i = 0
while i < (n-1):
# A valid pair is found
if (arr[i] == arr[i + 1]):
count += 1
# Skip the elements of
# the current pair
i = i + 2
# Current elements doesn't make
# a valid pair with any other element
else:
i += 1
return count
# Driver code
if __name__ == "__main__":
arr = [6, 5, 2, 3, 5, 2, 2, 1]
n = len(arr)
# Function call
print(cntgloves(arr, n))
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG {
// Function to return the maximum
// possible pairs of gloves
static int cntgloves(int[] arr, int n)
{
// To store the required count
int count = 0;
// Sort the original array
Array.Sort(arr);
for (int i = 0; i < n - 1;) {
// A valid pair is found
if (arr[i] == arr[i + 1]) {
count++;
// Skip the elements of
// the current pair
i = i + 2;
}
// Current elements doesn't make
// a valid pair with any other element
else {
i++;
}
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int[] arr = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = arr.Length;
// Function call
Console.WriteLine(cntgloves(arr, n));
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// Javascript implementation of the approach
// Function to return the maximum
// possible pairs of gloves
function cntgloves(arr, n)
{
// Sort the original array
arr.sort();
let res = 0;
let i = 0;
while (i < n) {
// take first number
let number = arr[i];
let count = 1;
i++;
// Count all duplicates
while (i < n && arr[i] == number) {
count++;
i++;
}
// If we spotted number just 2
// times, increment
// result
if (count >= 2) {
res = res + Math.floor(count / 2);
}
}
return res;
}
// Driver code
let arr = [6, 5, 2, 3, 5, 2, 2, 1];
let n = arr.length;
// Function call
document.write(cntgloves(arr, n));
// This code is contributed by susmitakundugoaldanga.
</script>
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Efficient Approach
1) Create an empty hash table (unordered_map in C++, HashMap in Java, Dictionary in Python)
2) Store frequencies of all elements.
3) Traverse through the hash table. For every element, find its frequency. Increment the result by frequency/2 for every element.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the maximum
// possible pairs of gloves
int cntgloves(int arr[], int n)
{
unordered_map<int,int> m;
// Traversing through the array
for(int i = 0; i < n; i++)
{
// Counting frequency
m[arr[i]] += 1;
}
// To store the required count
int count = 0;
for(auto it=m.begin();it!=m.end();it++)
{
count+=(it->second)/2;
}
return count;
}
// Driver code
int main()
{
int arr[] = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << cntgloves(arr, n);
return 0;
}
// This code was contributed by pushpeshrajdx01
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG{
public static int cntgloves(int arr[], int n)
{
HashMap<Integer,Integer> hm =new HashMap<>();
// Traversing through the array
for(int i = 0; i < n; i++)
{
// Counting frequency
hm.put(arr[i],hm.getOrDefault(arr[i],0)+1);
}
// To store the required count
int count = 0;
for(Map.Entry m : hm.entrySet())
{
int val=(int)m.getValue();
count+=val/2;
}
return count;
}
public static void main (String[] args) {
int arr[] = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = arr.length;
// Function call
System.out.println(cntgloves(arr, n));
}
}
Python3
# Python implementation of the approach
# Function to return the maximum
# possible pairs of gloves
def cntgloves(arr, n):
m = {}
# Traversing through the array
for i in arr:
# Counting frequency
if i not in m:
m[i] = 0
m[i] += 1
# To store the required count
count = 0
for it in m :
count += (m[it])//2
return count
# Driver code
def main():
arr = [6, 5, 2, 3, 5, 2, 2, 1]
n = len(arr)
# Function call
print(cntgloves(arr, n))
if __name__ == "__main__":
main()
# This code is contributed by shubhamsingh
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG{
public static int cntgloves(int[] arr, int n)
{
Dictionary<int,int> hm = new Dictionary<int,int>();
// Traversing through the array
for(int i = 0; i < n; i++)
{
// Counting frequency
if(hm.ContainsKey(arr[i]))
{
hm[arr[i]]= hm[arr[i]]+1;
}
else
hm.Add(arr[i], 1);
}
// To store the required count
int count = 0;
foreach(var m in hm)
{
int val = m.Value;
count += val/2;
}
return count;
}
static public void Main () {
int[] arr = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = arr.Length;
// Function call
Console.WriteLine(cntgloves(arr, n));
}
}
// This code is contributed by Aman Kumar
JavaScript
// Javascript implementation of the approach
// Function to return the maximum
// possible pairs of gloves
function cntgloves(arr, n)
{
var m = {};
// Traversing through the array
for(let i = 0; i < n; i++)
{
// Counting frequency
if (m[arr[i]] === undefined)
m[arr[i]] = 0
m[arr[i]] += 1;
}
// To store the required count
let count = 0;
for(let it in m)
{
count+=Math.floor(m[it]/2);
}
return count;
}
// Driver code
arr = [ 6, 5, 2, 3, 5, 2, 2, 1 ];
n = arr.length;
// Function call
console.log(cntgloves(arr, n));
// This code is contributed by Pushpesh Raj
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count unequal element pairs from the given Array 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, 1, 2} Output: 2 (1, 2) and (1, 2) are the only valid pairs.Input: arr[] = {1, 2, 3} Output: 3Input: arr[] = {1, 1, 1} Output: 0 Recommended
10 min read
Count of index pairs with equal elements in an array | Set 2 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: 4Ex
8 min read
Find the count of even odd pairs in a given Array Given an array arr[], the task is to find the count even-odd pairs in the array.Examples: Input: arr[] = { 1, 2, 1, 3 } Output: 2 Explanation: The 2 pairs of the form (even, odd) are {2, 1} and {2, 3}. Input: arr[] = { 5, 4, 1, 2, 3} Output: 3 Naive Approach: Run two nested loops to get all the poss
7 min read
Count equal pairs from given string arrays Given two string arrays s1[] and s2[]. The task is to find the count of pairs (s1[i], s2[j]) such that s1[i] = s2[j]. Note that an element s1[i] can only participate in a single pair. Examples: Input: s1[] = {"abc", "def"}, s2[] = {"abc", "abc"} Output: 1 Explanation: Only valid pair is (s1[0], s2[0
11 min read
Count pairs in an array such that both elements has equal set bits Given an array arr [] of size N with unique elements, the task is to count the total number of pairs of elements that have equal set bits count. Examples: Input: arr[] = {2, 5, 8, 1, 3} Output: 4 Set bits counts for {2, 5, 8, 1, 3} are {1, 2, 1, 1, 2} All pairs with same set bits count are {2, 8}, {
6 min read
Count of pairs having each element equal to index of the other from an Array Given an integer N and an array arr[] that contains elements in the range [1, N], the task is to find the count of all pairs (arr[i], arr[j]) such that i < j and i == arr[j] and j == arr[i]. Examples: Input: N = 4, arr[] = {2, 1, 4, 3} Output: 2 Explanation: All possible pairs are {1, 2} and {3,
6 min read