Sales by Match

Sort by

recency

|

349 Discussions

|

  • + 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;
        }
    
  • + 0 comments

    ` //// Python, using dict and flags:

    def sockMerchant(n, ar): no_pairs : int = 0 pair_nopair : dict[int, bool] = {}

    for sock in ar:
        if sock not in pair_nopair:
            pair_nopair[sock] = True
            continue
        if pair_nopair[sock]:
            no_pairs = no_pairs + 1
        pair_nopair[sock] = not pair_nopair[sock]
    
    return no_pairs
    

    `

  • + 0 comments

    Kotlin Solution

    fun sockMerchant(n: Int, ar: Array<Int>): Int {
            var numberOfPairs = 0
            ar.groupBy { it }.forEach {
                numberOfPairs += it.value.size / 2
            }
            return numberOfPairs
        }