Maximum types of candies a person can eat if only N / 2 of them can be eaten | 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 ; Given Input ; Function Call
function num_candyTypes ( candies ) { let s = new Set ( ) ; for ( let i = 0 ; i < candies . length ; i ++ ) { s . add ( candies [ i ] ) ; } return s . size ; } function distribute_candies ( candies ) { let allowed = candies . length / 2 ; let types = num_candyTypes ( candies ) ; if ( types < allowed ) document . write ( types ) ; else document . write ( allowed ) ; } let candies = [ 4 , 4 , 5 , 5 , 3 , 3 ] ; distribute_candies ( candies ) ;