• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remove a most frequent character in string, counted by index in alphabetical order

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which function help me to sort the string in alphabetical order?

find most occuring character("blablabla")
remove most occuring("aaa")
input: blablabla
output: "aaa" because the element h is in a higher position than the other characaters.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Johannes.

Johannes Smitth wrote:Post Today 21:03:45     Subject: Remove a most frequent character in string, counted by index in alphabetical order
Which function help me to sort the string in alphabetical order?


Well, you can't sort a string, as String is an immutable data type in Java. What you could do, is to construct another string, which may contain the same characters just in the order you want them.

So how that could be done - you could build and array of chars from the given string, then you could )]sort it, and build a new string from the sorted array of chars.

But before you do all that, would you like to explain us your full algorithm? That is what mostly matters as of now really.
 
Johannes Smitth
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.HashSet;

public class proge21 {
   public static void main(String[] args) {
       char[] arr = {'A', 'B', 'C', 'A', 'B','B'};

       HashSet<Character> hset = new HashSet<Character>();

       for (int i = 0; i < arr.length; i++) {
           hset.add(arr[i]);
       }

       Object[] ObjChar = hset.toArray();

       char[] resultArr = new char[ObjChar.length];

       for (int j = 0; j < ObjChar.length; j++) {

           resultArr[j] = (char) ObjChar[j];
       }
I am stuck with sorting
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using a Set? That looks as if you were guessing. You can guess 1,000,000× and there is a good chance that one guess will be correct. Or you can think about the problem and get it right first time
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you clear in what is being asked of you? I'm not even sure I understand what the requirements are. If the input is blablabla, then all three letters, 'b', 'l', and 'a' occur with the same frequency. How do you break the tie and pick 'a' as "the most frequently occurring" in this case?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:How do you break the tie and pick 'a' as "the most frequently occurring" in this case?


I think that is because 'a' occurs most frequently last if there are other element(s) which occur the same amount of times. But OP needs to clarify indeed, as I don't get from the quote below where that 'h' came from while we were talking about 'a'.

OP wrote:output: "aaa" because the element h is in a higher position than the other characaters.

 
Johannes Smitth
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Junilu Lacar wrote:How do you break the tie and pick 'a' as "the most frequently occurring" in this case?


I think that is because 'a' occurs most frequently last if there are other element(s) which occur the same amount of times. But OP needs to clarify indeed, as I don't get from the quote below where that 'h' came from while we were talking about 'a'.

OP wrote:output: "aaa" because the element h is in a higher position than the other characaters.



Exactly, it must read from a-z and print out the char with highest order. Forget that h, it was a typing error.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example still does not match the description. If you are supposed to remove all the last letter that occurs most frequently, the why is the output "aaa"? Wouldn't that mean you removed all the other letters from the input string?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The example still does not match the description. If you are supposed to remove all the last letter that occurs most frequently, the why is the output "aaa"? Wouldn't that mean you removed all the other letters from the input string?


Junilu is right on this one. I appear to misread the instructions too, probably same as OP.

@OP
Do you have instructions which you could copy/paste without adding any of your interpretation/re-wording?
 
Johannes Smitth
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, the issue has been resolved for a month now. Still thanks for helping me!
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic