Java O(n); Easy to understand solution


  • 0
    S
    public class Solution {
        public int distributeCandies(int[] candies) {
            HashSet<Integer> sister = new HashSet<Integer>();
            int diffCandies = 0;
            for (int i=0; i<candies.length; i++){
                if (!sister.contains(candies[i]) && diffCandies < (candies.length/2)){
                    sister.add(candies[i]);
                    diffCandies++;
                }            
            }
            return diffCandies;
        }
    }
    

Log in to reply
 

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