实现java字符串中找出出现次数最多的字符
package com.example.demo;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class StringTest {
public static void main(String[] args) {
String s = "abaaccddEEEEEEEEdDckebbcaaaaa";
fun(s);
}
public static void fun(String s) {
int[] a = new int[s.length()];
char[] chars = s.toCharArray();
int num = 0;
Map map=new HashMap<>();
for (int i = 0; i < chars.length; i++) {
if (a[i] == 0) {
int temp = 0;
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] == chars[j]) {
a[j] = 1;
temp++;
}
}
map.put(chars[i],temp+1);
if (temp > num) {
num = temp + 1;
}
}
}
Iterator iterator = map.entrySet().iterator();
StringBuffer buffer=new StringBuffer();
buffer.append("出现最多的字符:");
while (iterator.hasNext()){
Map.Entry next = (Map.Entry) iterator.next();
if (num==(int)next.getValue()){
buffer.append(next.getKey()+",");
}
}
buffer.deleteCharAt(buffer.lastIndexOf(","));
buffer.append("\t出现次数:"+num+"次");
System.out.println(buffer);
}
}
第一次写,有错请大佬指教