java calculates the number of occurrences of the letter in a given string and the number of occurrences of that letter
- 2020-06-03 06:28:03
- OfStack
This article shows how java calculates the number of occurrences of the letter in a given string and the number of occurrences of that letter. To share for your reference, specific as follows:
import Java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class TestStringSplict {
public static void main(String[] args){
String str = "aaaaaaaccccccccccccccccccccccaaaabb";
// with map implementation
// TreeMap<Character,Integer> map = new TreeMap<Character,Integer>();
// for(Character ch : str.toCharArray()){
// if((ch>='a' && ch<'z')||(ch>'A' && ch<'Z')){
// Integer count = map.get(ch);
// map.put(ch, null==count?1:count+1);
// }
// }
//
// System.out.println(Collections.max(map.values()));
// Using ordinary arrays
int[] aa = new int[60];
for(char temp:str.toCharArray()){
if((temp>=65 && temp<=90)||(temp>=97 && temp<=122)){
temp -= 65;
aa[temp]++;
}
}
int max = aa[0]; int position = 0;
for(int i=0;i<aa.length;i++){
if(aa[i]>max){
max = aa[i]; position = i;
}
}
System.out.println(max);
System.out.println(" The letter "+(char)(position+65) + " appear " + max + " time ");
}
}
PS: Here are some more online character statistics tools for your reference:
Online word count tool:
http://tools.ofstack.com/code/zishutongji
Online character Statistics and editing tools:
http://tools.ofstack.com/code/char_tongji
I hope this article has been helpful in java programming.