C# solution: dictionary


  • 0
    B
    public class Solution 
    {
        public int DistributeCandies(int[] candies) 
        {
            var candyAndCount = new Dictionary<int, int>();
    
            foreach(var candy in candies)
            {
                if (!candyAndCount.ContainsKey(candy))  candyAndCount[candy] = 0;
    
                candyAndCount[candy]++;
            }
    
            var numberOfKinds = candyAndCount.Count;
    
            var totalForSister = candies.Length / 2;
            
            return Math.Min(totalForSister, numberOfKinds);
        }
    }
    

Log in to reply
 

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