• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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: 64172
215
  • Mark post as helpful
  • send pies
  • 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
 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, the issue has been resolved for a month now. Still thanks for helping me!
 
Tell me how it all turns out. Here is a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!