Minimize sum of given array by removing all occurrences of a single digit | Java program for the above approach ; Function to remove each digit from the given integer ; Convert into string ; Stores final string ; Traverse the string ; Append it to the final string ; Return integer value ; Function to find the minimum sum by removing occurences of each digit ; Iterate in range [ 0 , 9 ] ; Traverse the array ; Update the minimum sum ; Print the minimized sum ; Driver Code
import java . util . * ; class GFG { static int remove ( int N , int digit ) { String strN = String . valueOf ( N ) ; String ans = " " ; for ( char i : strN . toCharArray ( ) ) { if ( ( i - '0' ) == digit ) { continue ; } ans += i ; } return Integer . parseInt ( ans ) ; } static void getMin ( int [ ] arr ) { int minSum = Integer . MAX_VALUE ; for ( int i = 0 ; i < 10 ; i ++ ) { int curSum = 0 ; for ( int num : arr ) curSum += remove ( num , i ) ; minSum = Math . min ( minSum , curSum ) ; } System . out . print ( minSum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 34 , 23 , 85 , 93 } ; getMin ( arr ) ; } }