Modify array by removing characters from their Hexadecimal representations which are present in a given string | java program for the above approach ; Function to convert a decimal number to its equivalent hexadecimal number ; Function to convert hexadecimal number to its equavalent decimal number ; Stores characters with their respective hexadecimal values ; Stores answer ; Traverse the string ; If digit ; If character ; Return the answer ; Function to move all the alphabets to front ; Function to modify each array element by removing characters from their hexadecimal representation which are present in a given string ; Traverse the array ; Stores hexadecimal value ; Remove the characters from hexadecimal representation present in string S ; Stores decimal value ; Replace array element ; Print the modified array ; Driver Code ; Given array ; Given string ; Function call to modify array by given operations
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String decHex ( int n ) { char alpha [ ] = { ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' } ; StringBuilder ans = new StringBuilder ( " " ) ; while ( n > 0 ) { if ( n % 16 < 10 ) { ans . append ( Integer . toString ( n % 16 ) ) ; } else { ans . append ( alpha [ n % 16 - 10 ] ) ; } n /= 16 ; } ans = ans . reverse ( ) ; return ans . toString ( ) ; } static int hexDec ( String convertedHex ) { char mp [ ] = { 10 , 11 , 12 , 13 , 14 , 15 } ; int ans = 0 ; int pos = 0 ; StringBuilder s = new StringBuilder ( convertedHex ) ; convertedHex = s . reverse ( ) . toString ( ) ; for ( char ch : convertedHex . toCharArray ( ) ) { if ( Character . isDigit ( ch ) ) { ans += ( ( int ) Math . pow ( 16 , pos ) ) * ( ch - '0' ) ; } else { ans += ( ( int ) Math . pow ( 16 , pos ) ) * mp [ ch - ' A ' ] ; } pos += 1 ; } return ans ; } static String removeChars ( String hexaVal , String S ) { HashSet < Character > setk = new HashSet < > ( ) ; for ( char ch : S . toCharArray ( ) ) { setk . add ( ch ) ; } String ans = " " ; for ( char ch : hexaVal . toCharArray ( ) ) { if ( setk . contains ( ch ) ) { continue ; } ans += ch ; } return ans ; } static void convertArr ( int arr [ ] , String S ) { for ( int i = 0 ; i < arr . length ; i ++ ) { String hexaVal = decHex ( arr [ i ] ) ; String convertedHex = removeChars ( hexaVal , S ) ; int decVal = hexDec ( convertedHex ) ; arr [ i ] = decVal ; } for ( int val : arr ) { System . out . print ( val + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 74 , 91 , 31 , 122 } ; String S = "1AB " ; convertArr ( arr , S ) ; } }