<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 程序:在`String`中查找重復的字符 > 原文: [https://beginnersbook.com/2014/07/java-program-to-find-duplicate-characters-in-a-string/](https://beginnersbook.com/2014/07/java-program-to-find-duplicate-characters-in-a-string/) 該程序將找出`String`中的重復字符并顯示它們的計數。 ```java import java.util.HashMap; import java.util.Map; import java.util.Set; public class Details { public void countDupChars(String str){ //Create a HashMap Map<Character, Integer> map = new HashMap<Character, Integer>(); //Convert the String to char array char[] chars = str.toCharArray(); /* logic: char are inserted as keys and their count * as values. If map contains the char already then * increase the value by 1 */ for(Character ch:chars){ if(map.containsKey(ch)){ map.put(ch, map.get(ch)+1); } else { map.put(ch, 1); } } //Obtaining set of keys Set<Character> keys = map.keySet(); /* Display count of chars if it is * greater than 1\. All duplicate chars would be * having value greater than 1. */ for(Character ch:keys){ if(map.get(ch) > 1){ System.out.println("Char "+ch+" "+map.get(ch)); } } } public static void main(String a[]){ Details obj = new Details(); System.out.println("String: BeginnersBook.com"); System.out.println("-------------------------"); obj.countDupChars("BeginnersBook.com"); System.out.println("\nString: ChaitanyaSingh"); System.out.println("-------------------------"); obj.countDupChars("ChaitanyaSingh"); System.out.println("\nString: #@[email?protected]!#$%!!%@"); System.out.println("-------------------------"); obj.countDupChars("#@[email?protected]!#$%!!%@"); } } ``` 輸出: ```java String: BeginnersBook.com ------------------------- Char e 2 Char B 2 Char n 2 Char o 3 String: ChaitanyaSingh ------------------------- Char a 3 Char n 2 Char h 2 Char i 2 String: #@[email?protected]!#$%!!%@ ------------------------- Char # 2 Char ! 3 Char @ 3 Char $ 2 Char % 2 ``` **參考:** [`HashMap`](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看