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