Java O(n) time, O(n) space


  • 0
    D
    public class Solution {
        public int distributeCandies(int[] candies) {
            int sizeForEach = candies.length / 2;
            Set<Integer> set = new HashSet<>();
            
            for (int i = 0; i < candies.length; i++) {
                set.add(candies[i]);
            }
            
            return set.size() >= sizeForEach ? sizeForEach : set.size();
        }
    }
    

Log in to reply
 

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