Sales by Match HackerRank Solution in C, C++, Java, Python

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

For example, there are n=7 socks with colors 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. It must return an integer representing the number of matching pairs of socks that are available.

sockMerchant has the following parameter(s):

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

Input Format

The first line contains an integer n, the number of socks represented in ar.

The second line contains n space-separated integers describing the colors ar[i] of the socks in the pile.

Constraints

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

Output Format

Return the total number of matching pairs of socks that Alex can sell.

Sample Input

9

10 20 20 10 10 30 50 10 20

Sample Output

3

 

Sales by Match HackerRank 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 HackerRank Solution in 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;
}

 

 

Sales by Match HackerRank 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) {

Scanner ask=new Scanner(System.in);
int[] colors=new int[101];
int ans=0;
int n=ask.nextInt();
for(int i=0;i<n;i++){
int color=ask.nextInt();
colors[color]++;
if(colors[color]%2 == 0){
ans++;
colors[color] = 0;
}
}
System.out.println(ans);
}
}

 

 

Sales by Match HackerRank 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 HackerRank Solution in C#

using System;
using System.Collections.Generic;
class Solution {
static void Main(String[] args) {
int n = int.Parse(Console.ReadLine());
int[] A = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
Array.Sort(A);
int ans = 0;
for (int i = 1; i < n; i++) {
if (A[i] == A[i - 1]) { ans++; i++; }
}
Console.WriteLine(ans);
}
}

 

Attempt – Sales by Match HackerRank Challenge

Link –  https://www.hackerrank.com/challenges/sock-merchant/

 

Leave a Comment