Partition array into minimum number of equal length subsets consisting of a single distinct value | 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
function CntOfSubsetsByPartitioning ( arr , N ) { var freq = { } ; for ( var i = 0 ; i < N ; i ++ ) { if ( freq . hasOwnProperty ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq [ arr [ i ] ] = 1 ; } } var freqGCD = 0 ; for ( const [ key , value ] of Object . entries ( freq ) ) { freqGCD = __gcd ( freqGCD , value ) ; } return parseInt ( N / freqGCD ) ; } function __gcd ( a , b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } var arr = [ 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 ] ; var N = arr . length ; document . write ( CntOfSubsetsByPartitioning ( arr , N ) ) ;