Sales by Match

Sort by

recency

|

340 Discussions

|

  • + 0 comments

    Java solution using a Integer, Boolean map. Idea is to negate whatever bool is found in the map for each value. If it isn't already in the map put false. Increment a counter when something is made true. // Write your code here HashMap map = new HashMap<>(); int pairs = 0; System.out.println(map); for(Integer i : ar){ map.put(i, !map.getOrDefault(i,true)); if(map.get(i) == true){pairs++;} } S }

    }

  • + 0 comments

    in Js:

    function sockMerchant(n, ar) {
        const pairsOfColors = ar.reduce((obj, num) => {
            obj[num] = (obj[num] || 0) + 1;
            return obj;
        }, {});
        let pairs = [];
        for (let key in pairsOfColors) {
            if (!(pairsOfColors[key] <= 1) && pairsOfColors[key] % 2 === 0) {
                pairs.push(pairsOfColors[key] / 2);
            } else if (!(pairsOfColors[key] <= 1) && pairsOfColors[key] % 2 === 1) {
                pairs.push((pairsOfColors[key] - 1) / 2);
            } else {
                pairs.push(0);
            }
        }
      return pairs.reduce((acc, num) => (acc + num), 0);
    }
    
  • + 0 comments

    C#

            var cc = ar.GroupBy(x => x)
                           .Select(g => new { Count = g.Count(), Number = g.Key })
                           .ToList();
    
            var pairs = cc.Where(x => x.Count >= 2).Sum(x => x.Count / 2);
            return pairs;
    
  • + 0 comments

    My asnwer C#

     int pairs = 0;
            ar.Sort();
    
            for (int i = 0; i < n - 1; i++)
            {
                if (ar[i] == ar[i + 1])
                {
                    pairs++;
                    i++;
                }
            }
    
            return pairs;
    
  • + 0 comments
    def sockMerchant(n, ar):
        return sum([math.floor(ar.count(s) / 2) for s in set(ar)])