Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | 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
function maxUniqueElements ( A , N ) { let mp = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( mp . has ( A [ i ] ) ) { mp . set ( mp . get ( A [ i ] ) , mp . get ( A [ i ] ) + 1 ) ; } else { mp . set ( A [ i ] , 1 ) ; } } let cnt = 0 ; for ( let [ key , value ] of mp ) { if ( value % 2 == 0 ) { cnt ++ ; } } let ans = mp . size ; if ( cnt % 2 == 1 ) { ans -- ; } return ans ; } let N = 5 ; let A = [ 1 , 2 , 1 , 3 , 7 ] ; document . write ( maxUniqueElements ( A , N ) ) ;