Minimum number of subsets with distinct elements | A sorting based solution to find the minimum number of subsets of a set such that every subset contains distinct elements . ; Function to count subsets such that all subsets have distinct elements . ; Take input and initialize res = 0 ; Sort the array ; Traverse the input array and find maximum frequency ; For each number find its repetition / frequency ; Update res ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int subset ( int ar [ ] , int n ) { int res = 0 ; Arrays . sort ( ar ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; for ( ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) count ++ ; else break ; } res = Math . max ( res , count ) ; } return res ; } public static void main ( String argc [ ] ) { int arr [ ] = { 5 , 6 , 9 , 3 , 4 , 3 , 4 } ; int n = 7 ; System . out . println ( subset ( arr , n ) ) ; } }