1-line JavaScript O(n) solution using Set


  • 6
    var distributeCandies = function(candies) {
        return Math.min(new Set(candies).size, candies.length / 2);
    };
    

  • 0
    J

    And with ES6 syntax you can write the same thing even shorter:

    const distributeCandies = candies => Math.min(new Set(candies).size, candies.length / 2);
    

    :)


Log in to reply
 

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