# HackerRank Sales by Match Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Sales by Match Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

## HackerRank Sales by Match

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.

## HackerRank Sales by Match Solution

### Sales by Match Solution in C

```#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

int n,i,t;
scanf("%d",&n);
int c[101];
memset(c,0,sizeof(c));
for(i=0;i<n;i++)
{
scanf("%d",&t);
c[t]++;
}
long int ans=0;
for(i=1;i<=100;i++)
{
ans+=(c[i]/2);
}
printf("%ld",ans);
return 0;
}```

### Sales by Match Solution in Cpp

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

using namespace std;
using LINT = long long int;
using PII = pair<int,int>;

#define PB push_back
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)

int socks[107];

int main() {
int n;
cin >> n;
REP(i,n){
int x;
cin>>x;
socks[x]++;
}

int ans = 0;
REP(i,107)ans+=socks[i]/2;
cout<<ans<<endl;

return 0;
}```

### Sales by Match Solution in 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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int[] colors=new int[101];
int ans=0;
for(int i=0;i<n;i++){
colors[color]++;
if(colors[color]%2 == 0){
ans++;
colors[color] = 0;
}
}
System.out.println(ans);
}
}```

### Sales by Match Solution in Python

```from collections import Counter

N = int(raw_input())

C = map(int, raw_input().split(' '))

c = Counter(C)
s = 0
for i in c:
s += c[i]/2
print s```

### Sales by Match Solution using JavaScript

```function processData(input) {
var pInput = input.split('\n');
var n = pInput[0];
var pInput = pInput[1].split(' ');
var oddItems = {};
pInput.map(function (i) {
if(oddItems[i]) {
delete oddItems[i];
} else {
oddItems[i] = true;
}
})
console.log((n - Object.keys(oddItems).length)/2);
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```

### Sales by Match Solution in Scala

```object Solution {

def main(args: Array[String]) {
val ans = socks.distinct.map(i => socks.count(_ == i)/2).sum;
println(ans)

}
}```

### Sales by Match Solution in Pascal

```var a:array[0..100]of longint;
i,n,x,kq:longint;
begin
for i:=1 to n do
begin
if a[x]mod 2=1 then inc(kq);
inc(a[x]);
end;
write(kq);
end.```

Disclaimer: This problem (Sales by Match) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

## FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.