Sales by Match Hacker Rank Solution Best & Easiest

Sales by Match Problem Solution
Sales by Match Problem Solution

In this post, we will solve the Sales by Match HackerRank Solution. This problem (Sales by Match) is a part of the HackerRank Problem Solving series.

Problem: https://www.hackerrank.com/challenges/three-month-preparation-kit-sock-merchant/problem

Sales by Match Hacker Rank Solution

Problem solution in JavaScript programming:

function sockMerchant(n, ar) {
    let pairs = 0;
  let duplicate = 0
  for(let i = 0; i < ar.length; i++){
    for(let j = 0; j < ar.length; j++){
      if(ar[i] === ar[j] && i != j){
        duplicate++;
        ar.splice(j,1)
        ar.splice(i,1)
        j = 0
        console.log(duplicate)
      } 
    }
  }
  return duplicate

}

Problem solution in Python programming:

def sockMerchant(n, ar):
    # Write your code here
    out=0
    x = set(ar)
    for i in x:
       out += int( ar.count(i) / 2 )
       
    return out

Problem solution in C# programming:

public static int sockMerchant(int n, List<int> ar)
{
    var hashTable = new HashSet<int>();
    var result = 0;
    foreach (var sock in ar)
    {
        if (!hashTable.Contains(sock))
        {
            hashTable.Add(sock);
        }
        else
        {
            hashTable.Remove(sock);
            result++;
        }
    }

    return result;
}

Problem solution in C++ programming:

int sockMerchant(int n, vector<int> ar) {
    int num = 0;
    int pair  = 0;
   for (int i = 0 ; i<ar.size()-1; i++) {
      num = ar[i];
      for (int j = i+1; j<ar.size(); j++) {
        if (num == ar[j]&& ar[i] !=0 && ar[j]!=0) {
          ar[i] = 0;
          ar[j] = 0;
          pair++;
          break;
        }
    }     
}

return pair;
 }