Subsequence pair from given Array having all unique and all same elements respectively | Function to find the maximum length of subsequences with given property ; To store the frequency ; Traverse the array to store the frequency ; M . size ( ) given count of distinct element in arr [ ] ; Traverse map to find max frequency ; Find the maximum length on the basis of two cases in the approach ; Driver Code ; Function call
function maximumSubsequence ( arr , N ) { let M = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( M . has ( arr [ i ] ) ) { M . set ( arr [ i ] , M . get ( arr [ i ] ) + 1 ) ; } else { M . set ( arr [ i ] , 1 ) ; } } let distinct_size = M . size ; let maxFreq = 1 ; for ( let it of M ) { maxFreq = Math . max ( maxFreq , it [ 1 ] ) ; } document . write ( Math . max ( Math . min ( distinct_size , maxFreq - 1 ) , Math . min ( distinct_size - 1 , maxFreq ) ) ) ; } let arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 , 5 ] ; let N = arr . length ; maximumSubsequence ( arr , N ) ;