# Sales by Match | HackerRank Solution

Hello coders, today we are going to solve Sales by Match HackerRank Solution which is a Part of HackerRank Algorithm Series.

There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

Example

n = 7
ar = [1, 2, 1, 2, 1, 3, 2]
There is one pair of colorÂ 1Â and one of colorÂ 2. There are three odd socks left, one of each color. The number of pairs isÂ 2.

Function Description

Complete the sockMerchant function in the editor below.

sockMerchant has the following parameter(s):

• int n: the number of socks in the pile
• int ar[n]: the colors of each sock

Returns

• int:Â the number of pairs

## Input Format

The first line contains an integerÂ n, the number of socks represented inÂ ar.
The second line containsÂ nÂ space-separated integers,Â ar[i], the colors of the socks in the pile.

## Constraints

• 1 <= n <= 100
• 1 <= ar[i] <= 100 where 0 <= i < n

Sample Input

``````STDIN                       Function
-----                       --------
9                           n = 9
10 20 20 10 10 30 50 10 20  ar = [10, 20, 20, 10, 10, 30, 50, 10, 20]``````

Sample Output

``3``

Explanation

There are three pairs of socks.

## Solution – Sales by Match

### C++

```#include <bits/stdc++.h>

#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)

#define LL long long
#define Ldouble long double
#define PI 3.1415926535897932384626

#define PII pair<int,int>
#define PLL pair<LL,LL>
#define mp make_pair
#define fi first
#define se second

using namespace std;

int n, c[105], x;

int main(){
scanf("%d", &n);
FI(i, 1, n){
scanf("%d", &x);
c[x]++;
}

int ans = 0;

FI(i, 1, 100) ans += c[i] / 2;
printf("%d\n", ans);
return 0;
}```

### Python

```import math
import os
import random
import re
import sys

# Complete the sockMerchant function below.
from collections import Counter
def sockMerchant(n, ar):
sum = 0
for val in Counter(ar).values():
sum += val//2
return sum

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

n = int(input())

ar = list(map(int, input().rstrip().split()))

result = sockMerchant(n, ar)

fptr.write(str(result) + '\n')

fptr.close()```

### Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int pairs = 0;
Set<Integer> unmatched = new HashSet<>();
for (int i = 0; i < n; ++i) {
int ci = in.nextInt();
if (unmatched.contains(ci)) {
unmatched.remove(ci);
++pairs;
}
else {