Java O(n) Solution using HashMap


  • 0
    E
    public class Solution {
        public int distributeCandies(int[] candies) {
            HashMap<Integer, Integer>map=new HashMap<Integer, Integer>();
            for(int i=0;i<candies.length;i++){
                if(map.containsKey(candies[i])){
                    map.put(candies[i],map.get(candies[i])+1);
                }
                else{
                    map.put(candies[i],1);
                }
            }
            return map.size()>candies.length/2?candies.length/2:map.size();
        }
    }
    

Log in to reply
 

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