Sales by Match

Sort by

recency

|

351 Discussions

|

  • + 0 comments
    from collections import Counter
    def sockMerchant(n, ar):
        # Write your code here
        count = 0
        socks = Counter(ar)
        for key, value in socks.items():
            if value >= 2:
                count += value//2
        
        return count 
    
  • + 0 comments

    from collections import Counter

    def sockMerchant(n, ar):

    count = Counter(ar)
    res = 0
    
    val = list(count.values())
    
    for i in val:
    
        if i >= 2:
            res += i//2
    
    return res
    
  • + 0 comments

    c++

    int sockMerchant(int n, vector<int> ar) {
        unordered_map<int, int> frequencyMap;
        
        int nPairs = 0;
        
        for(int i = 0; i < ar.size(); ++i){
            frequencyMap[ar[i]] += 1;
            if(frequencyMap[ar[i]] % 2 == 0){
                nPairs +=1;
            }
        }
        
        return nPairs;
    }
    
  • + 0 comments

    python

    def sockMerchant(n, ar):
        d={i:ar.count(i) for i in set(ar)}
        res=0
        for i in d:
            if d[i] >= 2:
                res = res + d[i]//2
        return res
    
  • + 0 comments

    C# Code

    public static int sockMerchant(int n, List<int> ar)
        {
            int pairCount = 0;
            
            var groupList = ar.Order()
                .GroupBy(e => e)
                .Select(e => new { SockId = e.Key, Count = e.Count() })
            .ToList();
            
            foreach(var sockGroup in groupList)
            {
                pairCount += sockGroup.Count / 2;
            }
            
            return pairCount;
        }