Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java - remove most frequent characters in a string!

    Hi, how do I sort the string in alphabetical order?

    find most occuring character("blablabla")
    remove most occuring("aaa")
    input: blablabla
    output: "aaa"

  2. #2
    New Coder
    Join Date
    Jan 2016
    Location
    Malaysia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here the code to sort String in alphabetical order

    Code:
    public class Example {
    
        // Method to sort a string alphabetically 
        public static String sortString(String inputString) {
            // convert input string to char array 
            char tempArray[] = inputString.toCharArray();
    
            // sort tempArray 
            Arrays.sort(tempArray);
    
            // return new sorted string 
            return new String(tempArray);
        }
    
        // Driver method 
        public static void main(String[] args) {
            String inputString = "blablabla";
            String outputString = sortString(inputString);
    
            System.out.println("Input String : " + inputString);
            System.out.println("Output String : " + outputString);
        }
    }

  3. #3
    New to the CF scene
    Join Date
    May 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Now that the sorting is solved above


    Let us pick the one with the most frequent character


    Code:
    public String frequentCharacter(String sortedString){
    char mostFrequent= sortedString.charAt(0);
    int frequency;
    while(sortedString.length()!=0){
    char currentChar=sortedString.charAt(0);
    if ( frequency<1+sortedString.lastIndexOf(""+currentChar)){
    
    mostFrequent=currentChar;
    frequency=1+sortedString.lastIndexOf(""+currentChar);
    
    }
    
    sortedString.substring(1+sortedString.lastIndexOf(""+currentChar));
    
    }
    
    System. out . printing(String.format("most frequent is %s and occurred in %s times ", mostFrequent, "" +frequency))
    
    
    return "" +mostFrequent;
    
    }
    Sent from my GIONEE S10 lite using Tapatalk
    Last edited by vinyl-junkie; May 4th, 2019 at 09:36 PM. Reason: added code tags

  4. #4
    New Coder
    Join Date
    Jan 2016
    Location
    Malaysia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi akeempositive, can you wrap your code using CODE tags next time?

    Thanks.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •