Length of array pair formed where one contains all distinct elements and other all same elements | C # implementation to Divide the array into two arrays having same size , one with distinct elements and other with same elements ; Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
using System ; using System . Collections ; class GFG { static int findMaxSize ( int [ ] a , int n ) { ArrayList frq = new ArrayList ( n + 1 ) ; for ( int i = 0 ; i <= n ; i ++ ) frq . Add ( 0 ) ; for ( int i = 0 ; i < n ; ++ i ) frq [ a [ i ] ] = ( int ) frq [ a [ i ] ] + 1 ; int maxfrq = int . MinValue ; for ( int i = 0 ; i < frq . Count ; i ++ ) { if ( maxfrq < ( int ) frq [ i ] ) { maxfrq = ( int ) frq [ i ] ; } } int dist = n + 1 ; for ( int i = 0 ; i < frq . Count ; i ++ ) { if ( ( int ) frq [ i ] == 0 ) { dist -- ; } } int ans1 = Math . Min ( maxfrq - 1 , dist ) ; int ans2 = Math . Min ( maxfrq , dist - 1 ) ; int ans = Math . Max ( ans1 , ans2 ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 2 , 4 , 1 , 4 , 3 , 4 } ; int n = arr . Length ; Console . Write ( findMaxSize ( arr , n ) ) ; } }