Extremely Simple JavaScript Solution O(n) complexity


  • 0
    P
    /**
     * @param {number[]} candies
     * @return {number}
     */
    var distributeCandies = function(candies) {
        let hashTable = {};
        for (let i = 0; i < candies.length; i++)
            hashTable[candies[i]] = 0;
        let len = parseInt(candies.length / 2);
        let keysCount = 0;
        for (let key in hashTable)
            keysCount++;
        if (keysCount >= len)
            return len;
        else 
            return keysCount;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.