The highest occurrence frequency of Java string computing characters.

Source: Internet
Author: User

The highest occurrence frequency of Java string computing characters.
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 );

// It is used to store each character that appears in the string without repeating
Set <String> set = new HashSet <String> ();
Int stringLength = stringArray. length;
For (int I = 0; I <stringLength; I ++ ){
Set. add (stringArray [I]);
}
// Remove an empty 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
Int index = tempStr. indexOf (s );
// Start the string from the next Index
TempStr = tempStr. substring (index + 1 );
// If the index exists, add 1 to the record value
Count ++;

} Else {
// If the index does not exist, assign a value of false to exit the loop.
Flag = false;
}

}
// To execute the next loop
Flag = true;
// Store the value in map to match the Character Frequency
Map. put (s, count );
// As it is the previous result, the value is set to zero and the string is changed back to the original string.
Count = 0;
TempStr = str;

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

// Convert it to an array
Integer [] stringCount = al. toArray (new Integer [] {});
Int countLength = stringCount. length;
// Sort in ascending order
Arrays. sort (stringCount );
// Obtain the maximum value of the array.
Int max = stringCount [countLength-1];
For (String s: set ){
For (int I = 0; I <countLength; I ++ ){
// If the map value 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 ");
}

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.