Equally divide into two sets such that one set has maximum distinct elements | Javascript program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Insert all the resources in the set There will be unique resources in the set ; return minimum of distinct resources and n / 2 ; Driver code
function distribution ( arr , n ) { let resources = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) resources . add ( arr [ i ] ) ; return Math . min ( resources . size , parseInt ( n / 2 , 10 ) ) ; } let arr = [ 1 , 1 , 2 , 1 , 3 , 4 ] ; let n = arr . length ; document . write ( distribution ( arr , n ) + " " ) ;