Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone
Hackerrank sales by match solution

HackerRank Sales by Match problem solution

YASH PAL, 31 July 20247 February 2026

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

HackerRank Sales by Match problem solution in Python.

#!/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()

Sales by Match problem solution in Java8.

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 Hackerrank Problems Solutions interview prepration kit AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes