Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Sales by Match problem solution

YASH PAL, 31 July 20249 August 2024

In this HackerRank Sales by Match problem in the Interview preparation kit, you need to Complete the sockMerchant function. it has the following parameter(s):

  • int n: the number of socks in the pile
  • int ar[n]: the colors of each sock
Hackerrank sales by match solution
Hackerrank sales by match solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java8 Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the sockMerchant function below.
def sockMerchant(n, ar):
    num = 0
    for i in range(0,n):
        gum = 1
        for j in range(i+1,n):
            if ar[i] == None:
                continue
            if ar[i] == ar[j] and gum ==1:
                num = num + 1
                gum = gum + 1
                ar[j] = None
            
    return num

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()

Problem solution in Java8 Programming.

import java.io.*;
import java.util.*;

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 s = new Scanner(System.in);
        int[] freq = new int[101];
        int n = s.nextInt();
        for(int i = 0; i < n; i++){
            int x = s.nextInt();
            freq[x]++;
        }
        int total = 0;
        for(int i = 1; i < 101; i++){
            total+=freq[i]/2;
        }
        System.out.println(total);
    }
}

 

Problem solution in C++ programming.

#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;
}

 

Problem solution in C programming.

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

int main() {

    int n;
    scanf("%d",&n);
    int arr[101];
    for(int i=0;i<101;i++){
        arr[i]=0;
    }
    for(int i=0;i<n;i++){
        int j;
        scanf("%d",&j);
        arr[j] += 1;
    }
    int total=0;
    for(int i=0;i<101;i++){
        if(arr[i]> 1){
            if(arr[i] % 2 == 0) total += (arr[i] / 2);
            else total += ((arr[i]-1) / 2);
        }
    }
    printf("%d",total);
    
    
    
    return 0;
}

 

Problem solution in JavaScript programming.

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);
});

 

coding problems solutions interview prepration kit AlgorithmsHackerRank

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes