code every day with me

coderduck

duccanhole

Posted on November 1, 2021

code every day with me

--DAY 13--

Hi, I am going to make #100DaysOfCode Challenge. Everyday I will try solve 1 problem from leetcode or hackerrank. Hope you can go with me until end.
Now let's solve problem today:

  • Problem: Sales by Match
  • Detail: here
  • Idea: sort then check each pair in array.
  • My solution (javascript):
function sockMerchant(n, ar) {
    let count=0,i=0;
    ar.sort((a,b)=>a-b);
    while(ar.length>=2){
        if(ar[0]==ar[1]){
            count++;
            ar.splice(0,2);
        }
        else{
            ar.shift();
        }
    }
    return count;
}
Enter fullscreen mode Exit fullscreen mode

-->If you have better solution or any question, please comment below. I will appreciate.

💖 💪 🙅 🚩
coderduck
duccanhole

Posted on November 1, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related