Java string calculation frequency appears highest character

Source: Internet
Author: User

public class Highfrequencyword {
public static void Findfrequencyword (String str) {
Collection<integer> al=new arraylist<integer> ();
Map<string,integer> map=new hashmap<string,integer> ();
String tempstr = str;
string[] Stringarray = Str.split ("");

System.out.println ("stringarray.length =" +stringarray.length);

Used to store every occurrence of a character in a string, and there is no duplicate
set<string> set = new hashset<string> ();
int stringlength = Stringarray.length;
for (int i = 0; i < stringlength; i++) {
Set.add (Stringarray[i]);
}
Remove a null character from the set
Set.remove ("");

SYSTEM.OUT.PRINTLN (set);
int count = 0;
Boolean flag = true;

for (String S:set) {

while (flag) {
If the index exists
if (Tempstr.indexof (s)! =-1) {
Record the current position of the character that appears
int index = Tempstr.indexof (s);
Let the string start at the next index
TempStr = tempstr.substring (index+1);
If the index exists, record the value plus 1
count++;

} else {
If the index does not exist, assign false to exit the loop
Flag = false;
}

}
In order to perform the next loop
Flag = true;
Put the value in map so that the character corresponds to its frequency
Map.put (S,count);
Because it is the previous result, the value is zeroed and the string is changed back to the original string
Count = 0;
TempStr = str;

}
Change the value of map to a list
Al= map.values ();

And then into the array
Integer[] Stringcount =al.toarray (new integer[]{});
int countlength=stringcount.length;
Sort by ascending
Arrays.sort (Stringcount);
Get the maximum value of the array
int max=stringcount[countlength-1];
for (String S:set) {
for (int i=0;i<countlength;i++) {
If the value of map is the same as the maximum value, the output
if (Map.get (s) ==max) {
SYSTEM.OUT.PRINTLN (S + ":" + max);
}
}

}
}

public static void Main (string[] args) {
Findfrequencyword ("Ababbsssss");
}

Java string calculation frequency appears highest character

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.