{"id":1386,"date":"2022-03-03T19:14:11","date_gmt":"2022-03-03T19:14:11","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1386"},"modified":"2022-03-03T19:14:11","modified_gmt":"2022-03-03T19:14:11","slug":"program-to-find-duplicate-characters-in-a-string","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/program-to-find-duplicate-characters-in-a-string\/","title":{"rendered":"Program to find duplicate Characters in a String"},"content":{"rendered":"\n<p>This program would find out the duplicate characters in a String and would display the count of them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.HashMap;\nimport java.util.Map;\nimport java.util.Set;\n \npublic class Details {\n \n  public void countDupChars(String str){\n \n    \/\/Create a HashMap \n    Map&lt;Character, Integer&gt; map = new HashMap&lt;Character, Integer&gt;(); \n \n    \/\/Convert the String to char array\n    char&#91;] chars = str.toCharArray();\n \n    \/* logic: char are inserted as keys and their count\n     * as values. If map contains the char already then\n     * increase the value by 1\n     *\/\n    for(Character ch:chars){\n      if(map.containsKey(ch)){\n         map.put(ch, map.get(ch)+1);\n      } else {\n         map.put(ch, 1);\n        }\n    }\n \n    \/\/Obtaining set of keys\n    Set&lt;Character&gt; keys = map.keySet();\n \n    \/* Display count of chars if it is\n     * greater than 1. All duplicate chars would be \n     * having value greater than 1.\n     *\/\n    for(Character ch:keys){\n      if(map.get(ch) &gt; 1){\n        System.out.println(\"Char \"+ch+\" \"+map.get(ch));\n      }\n    }\n  }\n \n  public static void main(String a&#91;]){\n    Details obj = new Details();\n    System.out.println(\"String: BeginnersBook.com\");\n    System.out.println(\"-------------------------\");\n    obj.countDupChars(\"BeginnersBook.com\");\n  \n    System.out.println(\"\\nString: ChaitanyaSingh\");\n    System.out.println(\"-------------------------\");\n    obj.countDupChars(\"ChaitanyaSingh\");\n \n    System.out.println(\"\\nString: #@$@!#$%!!%@\");\n    System.out.println(\"-------------------------\");\n    obj.countDupChars(\"#@$@!#$%!!%@\");\n  }\n}<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String: BeginnersBook.com\n-------------------------\nChar e 2\nChar B 2\nChar n 2\nChar o 3\n\nString: ChaitanyaSingh\n-------------------------\nChar a 3\nChar n 2\nChar h 2\nChar i 2\n\nString: #@$@!#$%!!%@\n-------------------------\nChar # 2\nChar ! 3\nChar @ 3\nChar $ 2\nChar % 2<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This program would find out the duplicate characters in a String and would display the count of them. Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1386"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1386"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1386\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}