## DEV Community is a community of 662,780 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Algorithm 202: Sock Merchant

NJOKU SAMSON EBERE
I love providing solutions to real world problems. When I am not coding, I enjoy movies, hanging out and working out.

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.

## Discussion (2)

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