给定一个段落作为输入,找到最常出现的字符。请注意,字符的大小写无关紧要。如果不止一个字符具有相同的最大出现频率,则返回所有字符我正在尝试这个问题,但最终一无所获。以下是我尝试过的代码,但它有很多我无法更正的错误:
public class MaximumOccuringChar {
static String testcase1 = "Hello! Are you all fine? What are u doing today? Hey Guyz,Listen! I have a plan for today.";
public static void main(String[] args)
{
MaximumOccuringChar test = new MaximumOccuringChar();
char[] result = test.maximumOccuringChar(testcase1);
System.out.println(result);
}
public char[] maximumOccuringChar(String str)
{
int temp = 0;
int count = 0;
int current = 0;
char[] maxchar = new char[str.length()];
for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
for (int j = i + 1; j < str.length(); j++)
{
char ch1 = str.charAt(j);
if (ch != ch1)
{
count++;
}
}
if (count > temp)
{
temp = count;
maxchar[current] = ch;
current++;
}
}
return maxchar;
}
}
原文由 IT_Philic 发布,翻译遵循 CC BY-SA 4.0 许可协议
您已经在这里得到答案: https ://stackoverflow.com/a/21749133/1661864
这是我能想到的最简单的方法。