Partition array into minimum number of equal length subsets consisting of a single distinct value | C # program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Recursive function to return gcd of a and b ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int CntOfSubsetsByPartitioning ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } } int freqGCD = 0 ; foreach ( KeyValuePair < int , int > i in freq ) { freqGCD = __gcd ( freqGCD , i . Value ) ; } return ( N ) / freqGCD ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int N = arr . Length ; Console . Write ( CntOfSubsetsByPartitioning ( arr , N ) ) ; } }