Subsequence pair from given Array having all unique and all same elements respectively | C # program for the above approach ; Function to find the maximum length of subsequences with given property ; To store the frequency ; Traverse the array to store the frequency ; M . Count 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
using System ; using System . Collections . Generic ; class GFG { static void maximumSubsequence ( int [ ] arr , int N ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( M . ContainsKey ( arr [ i ] ) ) { M [ arr [ i ] ] = M [ arr [ i ] ] + 1 ; } else { M . Add ( arr [ i ] , 1 ) ; } } int distinct_size = M . Count ; int maxFreq = 1 ; foreach ( KeyValuePair < int , int > m in M ) { maxFreq = Math . Max ( maxFreq , m . Value ) ; } Console . Write ( Math . Max ( Math . Min ( distinct_size , maxFreq - 1 ) , Math . Min ( distinct_size - 1 , maxFreq ) ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 , 5 } ; int N = arr . Length ; maximumSubsequence ( arr , N ) ; } }