Old style hashmap


  • 0
    A
    public int distributeCandies(int[] candies) {
            if(candies==null||candies.length==0)return 0;
            Map<Integer,Integer> stats = new HashMap<>();
            //collect stats
            for(int i:candies){
                if(stats.containsKey(i))
                    stats.put(i,stats.get(i)+1);
                else{
                    stats.put(i,1);
                }
            }
            return Math.min(candies.length/2,stats.size());
        }
    

Log in to reply
 

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