Remove all occurrences of any element for maximum array sum | C # program to convert fractional decimal to binary number ; Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxSumArray ( int [ ] arr , int n ) { int sum = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } int minimum = int . MaxValue ; foreach ( KeyValuePair < int , int > x in m ) minimum = Math . Min ( minimum , ( x . Value * x . Key ) ) ; return ( sum - minimum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( maxSumArray ( arr , n ) ) ; } }