Java HashSet easy solution


  • 0
    A
    public class Solution {
        public int distributeCandies(int[] candies) {
            HashSet<Integer> kinds = new HashSet<>();
            int nKinds = 0;
            
            for(int i = 0; i < candies.length; i++)
            {
                if(!kinds.contains(candies[i]))
                {
                    kinds.add(candies[i]);
                    nKinds++;
                }
            }
            
            return Math.min(nKinds, candies.length/2);
        }
    }
    

Log in to reply
 

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