3 line Java solution using new feature of java 8


  • 0
    M
    public class Solution {
        public int distributeCandies(int[] candies) {
            Set<Integer> hset = Arrays.stream(candies).boxed().collect(Collectors.toCollection(HashSet<Integer>::new));
            int num = candies.length / 2;
            return hset.size() > num ? num : hset.size();
        }
    }
    

    Using stream to convert input array to a hashSet.


Log in to reply
 

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