Count equal element pairs in the given array
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
#include <bits/stdc++.h>
using namespace std;
int cntgloves( int arr[], int n)
{
int count = 0;
sort(arr, arr + n);
for ( int i = 0; i < n - 1;) {
if (arr[i] == arr[i + 1]) {
count++;
i = i + 2;
}
else {
i++;
}
}
return count;
}
int main()
{
int arr[] = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntgloves(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int cntgloves( int arr[], int n)
{
Arrays.sort(arr);
int res = 0 ;
int i = 0 ;
while (i < n) {
int number = arr[i];
int count = 1 ;
i++;
while (i < n && arr[i] == number) {
count++;
i++;
}
if (count >= 2 ) {
res = res + count / 2 ;
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 6 , 5 , 2 , 3 , 5 , 2 , 2 , 1 };
int n = arr.length;
System.out.println(cntgloves(arr, n));
}
}
|
Python3
def cntgloves(arr, n):
count = 0
arr.sort()
i = 0
while i < (n - 1 ):
if (arr[i] = = arr[i + 1 ]):
count + = 1
i = i + 2
else :
i + = 1
return count
if __name__ = = "__main__" :
arr = [ 6 , 5 , 2 , 3 , 5 , 2 , 2 , 1 ]
n = len (arr)
print (cntgloves(arr, n))
|
C#
using System;
class GFG {
static int cntgloves( int [] arr, int n)
{
int count = 0;
Array.Sort(arr);
for ( int i = 0; i < n - 1;) {
if (arr[i] == arr[i + 1]) {
count++;
i = i + 2;
}
else {
i++;
}
}
return count;
}
public static void Main(String[] args)
{
int [] arr = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = arr.Length;
Console.WriteLine(cntgloves(arr, n));
}
}
|
Javascript
<script>
function cntgloves(arr, n)
{
arr.sort();
let res = 0;
let i = 0;
while (i < n) {
let number = arr[i];
let count = 1;
i++;
while (i < n && arr[i] == number) {
count++;
i++;
}
if (count >= 2) {
res = res + Math.floor(count / 2);
}
}
return res;
}
let arr = [6, 5, 2, 3, 5, 2, 2, 1];
let n = arr.length;
document.write(cntgloves(arr, n));
</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++
#include <bits/stdc++.h>
using namespace std;
int cntgloves( int arr[], int n)
{
unordered_map< int , int > m;
for ( int i = 0; i < n; i++)
{
m[arr[i]] += 1;
}
int count = 0;
for ( auto it=m.begin();it!=m.end();it++)
{
count+=(it->second)/2;
}
return count;
}
int main()
{
int arr[] = { 6, 5, 2, 3, 5, 2, 2, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntgloves(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static int cntgloves( int arr[], int n)
{
HashMap<Integer,Integer> hm = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
hm.put(arr[i],hm.getOrDefault(arr[i], 0 )+ 1 );
}
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;
System.out.println(cntgloves(arr, n));
}
}
|
Python3
def cntgloves(arr, n):
m = {}
for i in arr:
if i not in m:
m[i] = 0
m[i] + = 1
count = 0
for it in m :
count + = (m[it]) / / 2
return count
def main():
arr = [ 6 , 5 , 2 , 3 , 5 , 2 , 2 , 1 ]
n = len (arr)
print (cntgloves(arr, n))
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int cntgloves( int [] arr, int n)
{
Dictionary< int , int > hm = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (hm.ContainsKey(arr[i]))
{
hm[arr[i]]= hm[arr[i]]+1;
}
else
hm.Add(arr[i], 1);
}
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;
Console.WriteLine(cntgloves(arr, n));
}
}
|
Javascript
function cntgloves(arr, n)
{
var m = {};
for (let i = 0; i < n; i++)
{
if (m[arr[i]] === undefined)
m[arr[i]] = 0
m[arr[i]] += 1;
}
let count = 0;
for (let it in m)
{
count+=Math.floor(m[it]/2);
}
return count;
}
arr = [ 6, 5, 2, 3, 5, 2, 2, 1 ];
n = arr.length;
console.log(cntgloves(arr, n));
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...