Skip to content

Instantly share code, notes, and snippets.

@Nasah-Kuma
Created March 8, 2023 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nasah-Kuma/0bca6ba63fe7a2d48b464a35e82328b3 to your computer and use it in GitHub Desktop.
Save Nasah-Kuma/0bca6ba63fe7a2d48b464a35e82328b3 to your computer and use it in GitHub Desktop.
Solution to HackerRank's Sock Merchant Challenge: https://www.hackerrank.com/challenges/sock-merchant/problem?isFullScreen=true
/*
* Complete the 'sockMerchant' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER_ARRAY ar
*/
function sockMerchant(n, ar) {
// Write your code here
let socks = {};
let pairs = 0;
for (let element of ar) {
socks[element] = socks[element] + 1 || 1;
if (socks[element] % 2 === 0) {
pairs += 1;
}
}
return pairs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment