Java求字符串中出現次數最多的字符


Java求字符串中出現次數最多的字符

 

 

 【 尊重 原創,轉載請注明出處】http://blog.csdn.net/guyuealian/article/details/51933611
     Java求字符串中出現次數最多的字符,如String Str = "aaabbcddddee";那么輸出:d 4 ;若String Str = "aaabbcddddeexxxxxx";那么輸出:x 6
    【思路】:首先將字符串拆分為字符數組,然后轉存到HashMap集合中,該集合的key為字符串中出現的字符,value對應該字符出現的次數。最后只需要在HashMap集合中找到Value值最大的key即可。實現代碼如下(PS:輸出形式若有變化,自己修改哈)
[java]  view plain  copy
 
  1. import java.util.HashMap;  
  2. import java.util.Iterator;  
  3. import java.util.Map;  
  4. import java.util.Set;  
  5.   
  6. public class JavaTest {  
  7.     public static void main(String[] args) throws Exception {  
  8.         String Str = "AAbbcccaaaa";  
  9.         char[] StrArr = Str.toCharArray();// 把字符串轉為字符數組toCharArray  
  10.   
  11.         Map<Character, Integer> map = MapFunction(StrArr);  
  12.         char ch = FindMapMaxValue(map);  
  13.     }  
  14.   
  15.     /** 
  16.      * MapFunction:實現將字符數組轉存到Map中, 其中,Map中的key為出現的字符,value對應該字符出現的次數 
  17.      * @param StrArr  StrArr字符數組,輸入前必須先將字符串轉為字符數組 
  18.      * @return map 集合中,key為出現的字符(Character),value對應該字符出現的次數(Integer) 
  19.      */  
  20.     public static Map<Character, Integer> MapFunction(char[] StrArr) {  
  21.         Map<Character, Integer> map = new HashMap<Character, Integer>();  
  22.         if (!(StrArr == null || StrArr.length == 0))// 先判斷字符數組是否為空  
  23.             for (int i = 0; i < StrArr.length; i++)  
  24.                 if (null != map.get(StrArr[i]))  
  25.                     // 若不為空,說明已經存在相同字符,則Value值在原來的基礎上加1  
  26.                     map.put(StrArr[i], map.get(StrArr[i]) + 1);  
  27.                 else  
  28.                     map.put(StrArr[i], 1);  
  29.   
  30.         return map;  
  31.     }  
  32.   
  33.     /** 
  34.      * FindMapMaxValue 差找map中Value的最大值maxValue,類似於選擇排序尋找最大值的過程: 
  35.      * 先任取一個Value值定義為最大值,然后與之比較 
  36.      * @param map 輸入Map集合,該集合key為出現的字符(Character),value對應該字符出現的次數(Integer) 
  37.      * @return maxKey 返回出現次數最多的字符 
  38.      */  
  39.     public static Character FindMapMaxValue(Map<Character, Integer> map) {  
  40.         Set<Character> keys = map.keySet();// 獲得所有key值  
  41.         Iterator keys_Itera = keys.iterator();// 實例化Iterator  
  42.         // keys_Itera.next():依次獲得key值  
  43.         // map.get(key):獲得對應的value值  
  44.         Character maxKey = (Character) keys_Itera.next();// 定義第一個為最大value和對應的key  
  45.         int maxValue = map.get(maxKey);  
  46.   
  47.         while (keys_Itera.hasNext()) {  
  48.             Character temp = (Character) keys_Itera.next();  
  49.             if (maxValue < map.get(temp)) {  
  50.                 maxKey = temp;  
  51.                 maxValue = map.get(temp);  
  52.             }  
  53.         }  
  54.         System.out.println("出現次數最多的字符:" + maxKey + " 出現次數:" + maxValue);  
  55.         return maxKey;  
  56.     }  
  57. }  
上面的FindMapMaxValue方法,還可以使用Map.Entry提高效率,進一步優化為:
[java]  view plain  copy
 
  1. public static char FindMapMaxValue(Map<Character, Integer> map) {  
  2.   
  3.     Iterator iter = map.entrySet().iterator();  
  4.     Map.Entry entry = (Map.Entry) iter.next();// 將第一個entry定義為最大次數的  
  5.     char maxKey = (char) entry.getKey();// 獲得K  
  6.     int maxValue = (int) entry.getValue();// 獲得V  
  7.     while (iter.hasNext()) {  
  8.         entry = (Map.Entry) iter.next();// 第二個entry  
  9.         char tempK = (char) entry.getKey();  
  10.         int tempV = (int) entry.getValue();  
  11.         if (maxValue < tempV) {  
  12.             maxKey = tempK;  
  13.             maxValue = tempV;  
  14.         }  
  15.     }  
  16.   
  17.     System.out.println("出現次數最多的字符:" + maxKey + " 出現次數:" + maxValue);  
  18.     return maxKey;  
  19. }  


 
 
類似的還可以這樣:
[java]  view plain  copy
 
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. public class JavaTest {  
  4.     public static void main(String[] args) throws Exception {  
  5.         String Str = "aaabbcddddee";  
  6.         char[] StrArr = Str.toCharArray();// 把字符串轉為字符數組toCharArray  
  7.   
  8.         Map<Character, Integer> map = new HashMap<Character, Integer>();  
  9.         if (!(StrArr == null || StrArr.length == 0))// 先判斷字符數組是否為空  
  10.             for (int i = 0; i < StrArr.length; i++)   
  11.                 if (null != map.get(StrArr[i]))  
  12.         // 若不為空,說明已經存在相同字符,則Value值在原來的基礎上加1  
  13.                     map.put(StrArr[i], map.get(StrArr[i]) + 1);  
  14.                 else  
  15.                     map.put(StrArr[i], 1);  
  16.   
  17.         // 找map中Value的最大值maxValue,類似於選擇排序,尋找最大值的過程:  
  18.         // 先任取一個Value值定義為最大值,然后與之比較  
  19.         int maxValue = map.get(StrArr[0]);  
  20.         char ch = ' ';  
  21.         for (int j = 0; j < StrArr.length; j++)  
  22.             if (maxValue < map.get(StrArr[j])) {  
  23.                 maxValue = map.get(StrArr[j]);  
  24.                 ch = StrArr[j];  
  25.             }  
  26.   
  27.         System.out.println("現次數最多的字符:" + ch + " 出現次數:" + maxValue);  
  28.     }  
  29. }  
 
0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM