Count the number of times of a letter in a string. [Java Base]

Source: Internet
Author: User

Question: give you a string containing space and other punctuation marks. Calculate the most frequently used letters and the number of times the letter appears.
[Code ]:
Private static void totalTimes (String str ){
Char [] ch = str. toCharArray ();
Map <Character, Integer> timesMap = new HashMap <Character,
Integer> ();

For (int I = 0; I <ch. length; I ++ ){
If (ch [I]> = 65 & ch [I] <= 90) | (ch [I]> = 97
& Ch [I] <= 122 )){
Integer freq = timesMap. get (ch [I]);
TimesMap. put (ch [I], freq = null? Integer. valueOf (1): ++ freq );
}
}

Int charMaxIndex = Collections. max (timesMap. values ());
Int charMinIndex = Collections. min (timesMap. values ());

Set <Character> maxSet = new HashSet <Character> ();
Set <Character> minSet = new HashSet <Character> ();

For (Map. Entry <character, integer> entry: timesmap. entryset ()){

If (entry. getvalue (). Equals (charmaxindex )){
Maxset. Add (entry. getkey ());
}

If (entry. getvalue (). Equals (charminindex )){
Minset. Add (entry. getkey ());
}

}

System. Out. println ("the most frequently-seen letters:" + maxset );
System. Out. println ("occurrences:" + charmaxindex );
System. Out. println ("the smallest letter appears:" + minset );
System. Out. println ("occurrences:" + charminindex );

}
Of course, everyone except the above will have their own code! If the collection in Java cannot be used, it may be written on the array as follows:

  1. String STR =
    "Hello wolrd wlllkdsfhksadfls? Sdfls SDF. pqyutgvaaaxzsdfs"
    +
  2. "Lsdfj, ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"
    ;
  3. Int
    [] StrCounts =
    New
     
    Int
    [
    255
    ];
  4. Int
    BiggestCount =
    0
    ;
  5. Char
    BiggestCh =
    0
    ;
  6. Char
    Ch =
    0
    ;
  7. Int
    CurrentCount =
    0
    ;
  8. For
    (
    Int
    I = str. length ()-
    1
    ; I> =
    0
    ; I --){
  9. Ch = str. charAt (I );
  10. CurrentCount = ++ strCounts [ch];
  11. If
    (CurrentCount> biggestCount ){
  12. BiggestCount = currentCount;
  13. BiggestCh = ch;
  14. }
  15. }
  16. System. out. println (biggestCh );
  17. System. out. println (biggestCount );

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.