DEV Community 👩‍💻👨‍💻

duccanhole
duccanhole

Posted on

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.

Top comments (2)

Collapse
 
sherifhegazy profile image
sherifhegazy • Edited on

/** more memory usage but this first solution came to my mind */

function sockMerchant(n, ar) {
let pairnum=0;

let arr =Array(101).fill(0);
for (let i=0 ; i<n;i++){
      arr[ar[i]]++;

}
for (let i = 0 ; i<arr.length;i++){
    if (arr[i]>=2){

              pairnum+=Math.floor(arr[i]/2);

    }

}

return pairnum;
Enter fullscreen mode Exit fullscreen mode

}

Collapse
 
rammina profile image
Rammina

Good luck, I'm doing #100DaysofCode as well!
dev.to/rammina/100-days-of-code-an...

Take a look at this:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠