Java HashSet solution


  • 0
    T
    public class Solution {
        public int distributeCandies(int[] candies) {
            HashSet<Integer> set = new HashSet();
            int maxCandies = candies.length/2;
            int differentCandies = 0;
            
            for(int i = 0; i<candies.length;i++){
                if(!set.contains(candies[i])){
                    set.add(candies[i]);
                    differentCandies++;
                }
            }
            if(differentCandies<=maxCandies)
                return differentCandies;
            else return maxCandies;
        }
    }
    

Log in to reply
 

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