Sales by Match

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