<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":664,"date":"2021-07-14T00:03:09","date_gmt":"2021-07-14T00:03:09","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=664"},"modified":"2021-09-11T10:48:36","modified_gmt":"2021-09-11T10:48:36","slug":"find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/","title":{"rendered":"Find number of occurrence of each of the unique characters in a string ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\" class=\"color-pink\" style=\"text-align: justify;\">Find number of occurrence of each of the unique characters in a string ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>In this program an approach using\u00a0<a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-linked-hashmap\" target=\"_blank\" rel=\"noopener\">Hashmap<\/a>\u00a0in Java.\n<ul>\n<li>Declare a Hashmap in Java of {char, int}.<\/li>\n<li>Traverse in the string, check if the Hashmap already contains the traversed character or not.<\/li>\n<li>If it is present, then increase its count using\u00a0<a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/get-method-for-dictionaries-in-python\" target=\"_blank\" rel=\"noopener\">get()<\/a>\u00a0and\u00a0put()\u00a0function in Hashmap.<\/li>\n<li>Once the traversal is completed, traverse in the Hashmap and print the character and its frequency.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"example\" class=\"color-purple\">Example<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">Input: str = &quot;Wikitechy&quot;<br\/>Output: <br\/>        W 1<br\/>        i 2<br\/>        k 1<br\/>        t 1<br\/>        e 1<br\/>        c 1<br\/>        h 1<br\/>        y 1<\/code><\/pre> <\/div>\n<h2 id=\"sample-code-in-java\" class=\"color-blue\">Sample Code in Java<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java prorgam to count frequencies of <br\/>\/\/ characters in string using Hashmap <br\/>import java.io.*; <br\/>import java.util.*; <br\/>class OccurenceOfCharInString { <br\/>\tstatic void characterCount(String inputString) <br\/>\t{ <br\/>\t\t\/\/ Creating a HashMap containing char <br\/>\t\t\/\/ as a key and occurrences as a value <br\/>\t\tHashMap&lt;Character, Integer&gt; charCountMap = new HashMap&lt;Character, Integer&gt;(); <br\/><br\/>\t\t\/\/ Converting given string to char array <br\/><br\/>\t\tchar[] strArray = inputString.toCharArray(); <br\/><br\/>\t\t\/\/ checking each char of strArray <br\/>\t\tfor (char c : strArray) { <br\/>\t\t\tif (charCountMap.containsKey(c)) { <br\/><br\/>\t\t\t\t\/\/ If char is present in charCountMap, <br\/>\t\t\t\t\/\/ incrementing it&#039;s count by 1 <br\/>\t\t\t\tcharCountMap.put(c, charCountMap.get(c) + 1); <br\/>\t\t\t} <br\/>\t\t\telse { <br\/><br\/>\t\t\t\t\/\/ If char is not present in charCountMap, <br\/>\t\t\t\t\/\/ putting this char to charCountMap with 1 as it&#039;s value <br\/>\t\t\t\tcharCountMap.put(c, 1); <br\/>\t\t\t} <br\/>\t\t} <br\/><br\/>\t\t\/\/ Printing the charCountMap <br\/>\t\tfor (Map.Entry entry : charCountMap.entrySet()) { <br\/>\t\t\tSystem.out.println(entry.getKey() + &quot; &quot; + entry.getValue()); <br\/>\t\t} <br\/>\t} <br\/><br\/>\t\/\/ Driver Code <br\/>\tpublic static void main(String[] args) <br\/>\t{ <br\/>\t\tString str = &quot;Wikitechy&quot;; <br\/>\t\tcharacterCount(str); <br\/>\t} <br\/>} <\/code><\/pre> <\/div>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">Output<br\/>c 1<br\/>t 1<br\/>e 1<br\/>W 1<br\/>h 1<br\/>i 2<br\/>y 1<br\/>k 1<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : In this program an approach using Hashmap&#8230;.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[4004],"tags":[195,491,360,203,199,214,4304,198,363,209,15938,4252,205,2936,222,4253,4311,196,212,213,4307,4300,4297,4303,4266,4298,4302,4299,286,207,366,4309,204,217,282,4310,4023,483,206,4306,4305,4308,4301,200,197,280,364,968],"class_list":["post-664","post","type-post","status-publish","format-standard","hentry","category-java","tag-accenture-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-atos-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-charat-java","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-technologies-interview-questions-and-answers","tag-compro-technologies-interview-questions-and-answers","tag-count-occurrences-of-character-in-string-java-using-hashmap","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-fis-global-business-solutions-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-how-to-count-characters-in-string-array-in-java","tag-how-to-count-occurrence-of-a-given-character-in-a-string-in-c","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-infosys-technologies-interview-questions-and-answers","tag-java-counter","tag-java-replace","tag-java-string","tag-java-string-compare","tag-java-string-contains","tag-java-string-length","tag-java-string-replace","tag-java-trim","tag-lt-infotech-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-occurrence-of-each-character-in-string-java","tag-oracle-corporation-interview-questions-and-answers","tag-peoplestrong-interview-questions-and-answers","tag-persistent-systems-interview-questions-and-answers","tag-program-to-find-occurrence-of-a-character-in-a-string-in-java","tag-rbs-india-de-interview-questions-and-answers","tag-reliance-industries-ltd-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-string-class-in-java","tag-string-class-methods-in-java","tag-string-count","tag-substring-java","tag-tech-mahindra-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-virtusa-consulting-services-pvt-ltd-interview-questions-and-answers","tag-wells-fargo-interview-questions-and-answers","tag-wipro-infotech-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find number of occurrence of each of the unique characters in a string ?<\/title>\n<meta name=\"description\" content=\"Find number of occurrence of each of the unique characters in a string ? - In this program an approach using Hashmap in Java.Declare a Hashmap in Java of {char, int}.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find number of occurrence of each of the unique characters in a string ?\" \/>\n<meta property=\"og:description\" content=\"Find number of occurrence of each of the unique characters in a string ? - In this program an approach using Hashmap in Java.Declare a Hashmap in Java of {char, int}.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-14T00:03:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-11T10:48:36+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/\",\"name\":\"Find number of occurrence of each of the unique characters in a string ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-14T00:03:09+00:00\",\"dateModified\":\"2021-09-11T10:48:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"Find number of occurrence of each of the unique characters in a string ? - In this program an approach using Hashmap in Java.Declare a Hashmap in Java of {char, int}.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find number of occurrence of each of the unique characters in a string ?","description":"Find number of occurrence of each of the unique characters in a string ? - In this program an approach using Hashmap in Java.Declare a Hashmap in Java of {char, int}.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/","og_locale":"en_US","og_type":"article","og_title":"Find number of occurrence of each of the unique characters in a string ?","og_description":"Find number of occurrence of each of the unique characters in a string ? - In this program an approach using Hashmap in Java.Declare a Hashmap in Java of {char, int}.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/","og_site_name":"Wikitechy","article_published_time":"2021-07-14T00:03:09+00:00","article_modified_time":"2021-09-11T10:48:36+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/","name":"Find number of occurrence of each of the unique characters in a string ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-14T00:03:09+00:00","dateModified":"2021-09-11T10:48:36+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"Find number of occurrence of each of the unique characters in a string ? - In this program an approach using Hashmap in Java.Declare a Hashmap in Java of {char, int}.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/java\/find-number-of-occurrence-of-each-of-the-unique-characters-in-a-string\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=664"}],"version-history":[{"count":4,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/664\/revisions"}],"predecessor-version":[{"id":3396,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/664\/revisions\/3396"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}