Java HashSet solution


  • 0
    E
    class Solution {
        public int distributeCandies(int[] candies) {
            Set<Integer> kinds = new HashSet<>();
            for (int i : candies)
                kinds.add(i);
            
            if (kinds.size() < candies.length / 2)
                return kinds.size();
            else
                return candies.length / 2;
        }
    }
    

Log in to reply
 

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