Created
March 8, 2023 06:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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