## DEV Community

NJOKU SAMSON EBERE

Posted on

# Algorithm 202: Sock Merchant

The Sock Merchant algorithm, is one of the easiest questions on hackerrank.

## Solution

``````// Complete the sockMerchant function below.
function sockMerchant(n, ar) {
let paired = [];
let uniqueNum = [...new Set(ar)];

uniqueNum.forEach((num) => {
let chunk = [];
for(let char of ar){
if(char === num && chunk.length < 2){
chunk.push(char);
if(chunk.length === 2){
paired.push(chunk);
chunk = [];
}
}
}
})

return paired.length
}
``````

### Conclusion

There are many ways to solve problems programmatically. Check the following articles for ideas on other ways to solve this algorithm question:

I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

You can also follow and message me on social media platforms.

Sabin Pandelovitch

Please be aware that by posting content from hackerrank here you have violated their terms and conditions. At least that's what I understood from their terms and conditions. Maybe I'm wrong