HackerRank Sales by Match Solution

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

HackerRank Sales by Match Solution
HackerRank Sales by Match Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Sales by Match Solution

Task

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

sock.png

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. */
        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);
    }
}
Ezoicreport this ad

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) {
    //Enter your code here
  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 n = io.StdIn.readInt;
        val socks = io.StdIn.readLine.split(" ").map(_.toLong);
        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
readln(n);
for i:=1 to n do
    begin
    read(x);
    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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Number Line Jumps solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad