/**
* ★查询给定字符串中,出现最多的字符和出现的次数
* 我在笔试的时候也几乎这么写的。但是我们技术总监说,这个效率是比较差的。
* 如果20个字符,有一个字符出现了11次,那么这个字符就是最多的。
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CharCount {
/*public static void Charcount(String string) {
if (string == null)
return;
int[] count = new int[string.length()];
for (int i = 0; i < count.length; i++) {
// 将字符串中索引字符存在临时变量中
char mid = string.charAt(i);
for (int j = 0; j < count.length; j++) {
if (mid == string.charAt(j))
count[i]++;
}
}
// 得到次数最多的字符
int index = 0;
int max = count[0];
for (int i = 0; i < count.length; i++) {
if (max < count[i]) {
max = count[i];
index = i;
}
}
System.out.println();
System.out.println("字符=" + string.charAt(index));
System.out.println("次数=" + count[index]);
}
*/
public static void main(String[] args) {
//Charcount("ABAACGDHGSTHJHH");
//findChar("ABAACGDHGSTHJHH");
findMaxCountChar("ABAACGDHGSTHJHH");
}
/*public static void Charcount(String string) {
//ArrayList 保存字符串中出现的字符
ArrayList list=new ArrayList(string.length());
for(int i=0;i<string.length();i++){
//只要表中不包含,将它添加到表中
if(!list.contains(string.charAt(i))){
list.add(string.charAt(i));
}
}
int[] count = new int[list.size()];
//这里外循环就不用遍历整个字符串,仅仅按照List表中所保存的字符进行遍历
for(int i=0;i<list.size();i++){
char mid=(Character) list.get(i);
for(int j=0;j<string.length();j++){
if(mid==string.charAt(j)){
count[i]++;
}
}
}
int index = 0;
int max = count[0];
for (int i = 0; i < count.length; i++) {
if (max < count[i]) {
max = count[i];
index = i;
}
}
System.out.println("字符串中出现的字符:");
for(int k=0;k<list.size();k++)
{
System.out.print(list.get(k)+" ");
}
System.out.println();
System.out.println("字符" + list.get(index)+"出现的次数最多");
System.out.println("共出现次数为: " + count[index]);
}*/
/*public static void findChar(String msg){
if(null == msg)
return;
Map<String,String> charMap = new HashMap<String,String>();
int base = 0;
String maxChar = "";
for(int i=0; i < msg.length(); i++){
String word = String.valueOf(msg.charAt(i));
if(!charMap.containsKey(word)){
charMap.put(word, "1");
}else{
int index = Integer.valueOf(charMap.get(word)) + 1;
charMap.put(word, String.valueOf(index));
if(index > base){
base = index;
maxChar = word;
}
}
}
// 出现最多次数
System.out.println(base);
// 出现做多次数字符
System.out.println(maxChar);
}*/
public static void findMaxCountChar(String str) {
if (str == null || str == "")
return;
Map<String, Integer> map = new HashMap<String, Integer>();
int maxCount = 0;
String maxCountStr = "";
List<String> list = new ArrayList<String>();
for (int i = 0; i < str.length(); i++) {
String key = String.valueOf(str.charAt(i));
if (maxCount >= str.length() / 2) {
if (maxCountStr == key) {
maxCount++;
}
} else {
if (!map.containsKey(key)) {
map.put(key, 1);
} else {
int count = map.get(key);
map.put(key, count + 1);
if ((count + 1) > maxCount) {
maxCount = count + 1;
maxCountStr = key;
list.clear();
list.add(key);
} else if ((count + 1) == maxCount) {
list.add(key);
}
}
}
}
System.out.println("最大次数" + maxCount);
if (maxCount >= str.length() / 2) {
System.out.println("字符" + maxCountStr);
} else {
System.out.println("字符" + list);
}
}
}
转载:http://www.cnblogs.com/java-class/archive/2013/04/25/3043444.html#2666696
感谢分享,一起成长!
欢迎评论,交流。