Sort by

recency

|

6049 Discussions

|

  • + 0 comments

    I want to recieve feedback about my solution in Javascript. How can I do it better?:

    function sockMerchant(n, ar) {
        ar.sort((a, b)=> a - b)
        let pairs=0;
        for(let i=0;i<ar.length;i++){
            if(ar[i]===ar[i+1]){
                pairs++;
                i++;
            }
        }
        return pairs;
    }
    
  • + 0 comments

    Java

    Map<Integer, Integer> check = new HashMap<>();
            AtomicInteger rs = new AtomicInteger(0);
            ar.forEach(it -> {
                if (check.containsKey(it)) {
                    rs.getAndIncrement();
                    check.remove(it);
                } else {
                    check.put(it, it);
                }
            });
            return rs.get();
    
  • + 0 comments

    C# Solution

        public static int sockMerchant(int n, List<int> ar)
        {
            Dictionary<int, int> pairs = new Dictionary<int, int>();
            
            for (int i = 0; i < n; i++) {
                if (pairs.ContainsKey(ar[i])) pairs[ar[i]]++;
                else pairs[ar[i]] = 1;
            }
            
            int sum = 0;
            foreach (KeyValuePair<int, int> pair in pairs) {
                sum += pair.Value / 2;
            }
            
            return sum;
        }
    

    Grouping up each socks and then just divide each group by 2. In C#, when a mathematical operation would result in a floating-point number for integer, the decimal would be concatenated. So if Sock 1 has 5 socks, divided by 2 it would result in 2 pairs - which is correct. Probably could optimize the loop further but the idea was there.

  • + 0 comments

    def sockMerchant(n, ar): # Write your code here count=0 y= Counter(ar) for i in y.values(): t=int(i/2) count+=t * return count

  • + 0 comments

    def sockMerchant(n, ar): # Write your code here count=0 y= Counter(ar) for i in y.values(): t=int(i/2) count+=t * return count