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;
}
``````

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

/** 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;
``````

}

Rammina

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