DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on

Algorithm 202: Sock Merchant

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

Question

Please refer to hakerrank website

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
}
Enter fullscreen mode Exit fullscreen mode

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.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (2)

Collapse
 
sabbin profile image
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

Ownership; Proprietary Rights.

HackerRank is owned and operated by Interviewstreet Incorporated. The visual interfaces, graphics, design, compilation, information, computer code, products, software (including any downloadable software), services, and all other elements of HackerRank provided by Interviewstreet (“Materials”) are protected by United States copyright, trade dress, patent, and trademark laws, international conventions, and all other relevant intellectual property and proprietary rights, and applicable laws. Except for any third party content or Content uploaded by You, all Materials are the copyrighted property of Interviewstreet or its subsidiaries or affiliated companies and/or third party licensors. All trademarks, service marks, and trade names are proprietary to Interviewstreet or its affiliates and/or third party licensors. Except as expressly authorized by Interviewstreet, You agree not to sell, license, distribute, copy, modify, publicly perform or display, transmit, publish, edit, adapt, create derivative works from, or otherwise make unauthorized use of the Materials.

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Thanks for bringing this to my notice. Let me make necessary re-adjustments