# 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
(arr[1], arr[4]) and (arr[2], arr[5]) are the only possible pairs.

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 equal to the element next to it then it is a valid pair and skip these two elements. Else the current element doesn’t make a valid pair with any other element and hence only skip the current element.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `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]); ` ` `  `    ``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) ` `{ ` `    ``// To store the required count ` `    ``int` `count = ``0``; ` ` `  `    ``// Sort the original array ` `    ``Arrays.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; ` ` `  `    ``System.out.println(cntgloves(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## 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);  ` ` `  `    ``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; ` ` `  `    ``Console.WriteLine(cntgloves(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```2
```

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,

My Personal Notes arrow_drop_up

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.