text: Maximum types of candies a person can eat if only N / 2 of them can be eaten | C # program for the above approach ; Function to find number of candy types ; Declare a hashset to store candies ; Traverse the given array and inserts element into set ; Return the result ; Function to find maximum number of types of candies a person can eat ; Store the number of candies allowed to eat ; Store the number of candy types ; Return the result ; Driver code ; Given Input ; Function Call
code: using System ; using System . Collections . Generic ; public class GFG { public static int num_candyTypes ( int [ ] candies ) { Dictionary < int , int > s = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < candies . Length ; i ++ ) { if ( ! s . ContainsKey ( candies [ i ] ) ) s . Add ( candies [ i ] , 1 ) ; } return s . Count ; } public static void distribute_candies ( int [ ] candies ) { int allowed = candies . Length / 2 ; int types = num_candyTypes ( candies ) ; if ( types < allowed ) Console . WriteLine ( types ) ; else Console . WriteLine ( allowed ) ; } static public void Main ( ) { int [ ] candies = { 4 , 4 , 5 , 5 , 3 , 3 } ; distribute_candies ( candies ) ; } }