Remove all occurrences of any element for maximum array sum | Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Drivers code
function maxSumArray ( arr , n ) { var sum = 0 ; var mp = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( mp . has ( arr [ i ] ) ) mp . set ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) else mp . set ( arr [ i ] , 1 ) } var minimum = 1000000000 ; mp . forEach ( ( value , key ) => { minimum = Math . min ( minimum , value * key ) ; } ) ; return ( sum - minimum ) ; } var arr = [ 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 ] ; var n = arr . length ; document . write ( maxSumArray ( arr , n ) ) ;