• + 0 comments

    Typescript

    function sockMerchant(n: number, ar: number[]): number {
       
       const socks = ar.sort((a,b)=>a-b);
       
       let result = 0;
       let last = socks[0];
       let pair = 0;
       
       for(let sock of socks) {
           if(sock === last) pair++;
           else { 
              result += pair%2 === 0 ? (pair)/2 : (pair-1)/2;
              
              pair = 1;
           }
               
           last = sock;
       }
    
       result += pair%2 === 0 ? (pair)/2 : (pair-1)/2;
    
       return result;
    }