Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | C # Program to implement the above approach ; Function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; Stores the frequency of array elements ; Traverse the array ; Iterate through the map ; If frequency of current element is even ; Stores the required count of unique elements remaining ; If count is odd ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxUniqueElements ( int [ ] Arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( Arr [ i ] ) ) { mp [ Arr [ i ] ] += 1 ; } else { mp [ Arr [ i ] ] = 1 ; } } int cnt = 0 ; foreach ( KeyValuePair < int , int > entry in mp ) { if ( ( entry . Value ) % 2 == 0 ) { cnt ++ ; } } int ans = mp . Count ; if ( cnt % 2 == 1 ) { ans -- ; } return ans ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] A = { 1 , 2 , 1 , 3 , 7 } ; Console . Write ( maxUniqueElements ( A , N ) ) ; } }