Java to find the highest frequency character in a string

Source: Internet
Author: User
Tags set set

The previous period of contact with this topic, generally understand, there are still some small places to further taste, for the time being noted.

Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.HashSet;ImportJava.util.Map;ImportJava.util.Set;/** Find the most frequently occurring characters in a string * main idea: first put the string into the set set to ensure that there are no duplicate characters * and then take out each character in the set set and index it with the indexof () method of String * If the index is not-1, the character exists, remember Record its index position in the string * and the variable + + that records the number of occurrences of the character, and then index from the current index position +1, * until the index value is-1, exit the loop, and the frequency at which each character appears is recorded * Next use map to store the frequency of characters and characters * The values of map are then formed into a ArrayList set * The maximum frequency calculated is compared to the value in map * If equal, it is the maximum frequency of the character*/  Public classHighfrequencyword { Public Static voidFindfrequencyword (String str) {Collection<Integer> al=NewArraylist<integer>(); Map<String,Integer> map=NewHashmap<string,integer>(); String TempStr= str;//Temporary storage stringstring[] Stringarray = Str.split ("");//cut a string into characters//to store characters that appear in a string without repeatingSet<string> set =NewHashset<string>(); intStringlength =stringarray.length;  for(inti = 0; i < stringlength; i++) {Set.add (stringarray[i]); }                 //remove a null character from the set (which one?) What do you mean?? )Set.remove (""); SYSTEM.OUT.PRINTLN (set);//the output here is [, A, B, c]                intCount = 0; BooleanFlag =true;  for(String s:set) { while(flag) {if(Tempstr.indexof (s)! =-1) {//if the index exists                    intindex = Tempstr.indexof (s);//record the current position of the characterTempStr = tempstr.substring (index + 1);//Continue queryingcount++; } Else{flag=false;//If the index does not exist, assign false to exit the loop}} Flag=true;//in order to perform the next loopMap.put (S,count);//record characters and their corresponding frequencies, and store them in a map//Initialize the results to prepare for the next cycleCount = 0; TempStr=str; }                 //Convert the value of map to a listAl=map.values (); //To the array againInteger[] Stringcount =al.toarray (Newinteger[]{}); Arrays.sort (stringcount);//Sort by Ascending                intCountlength=stringcount.length; intMAX=STRINGCOUNT[COUNTLENGTH-1];//get the maximum value of the array (the last one is the most frequently occurring value from small to large)                 for(String s:set) { for(inti=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);//Q: So the loop result will be output three times, how to output only once??                 }             }         }            }          Public Static voidMain (string[] args) {Findfrequencyword ("Abab BC"); } } 

Original: http://www.2cto.com/kf/201205/133219.html

Java to find the highest frequency character in a string

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.