how to find the frequency of characters in given string in java

Code:

import java.util.*;
import java.lang.*;
class Demo{
public static void main(String[] args){
String name = “idiot”;
Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0;i<name…


This content originally appeared on DEV Community and was authored by NJ

Code:

import java.util.*;
import java.lang.*;
class Demo{
    public static void main(String[] args){
        String name = "idiot";
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        for(int i=0;i<name.length();i++){
            Character ch = name.charAt(i);
            map.put(ch,map.getOrDefault(ch,0)+1);
        }
        System.out.println(map);
        for(Map.Entry<Character,Integer> e : map.entrySet()){
            System.out.println(e.getKey()+" "+e.getValue());
        }
    }   

}

o/p:

Image description


This content originally appeared on DEV Community and was authored by NJ


Print Share Comment Cite Upload Translate Updates
APA

NJ | Sciencx (2023-04-27T02:54:51+00:00) how to find the frequency of characters in given string in java. Retrieved from https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/

MLA
" » how to find the frequency of characters in given string in java." NJ | Sciencx - Thursday April 27, 2023, https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/
HARVARD
NJ | Sciencx Thursday April 27, 2023 » how to find the frequency of characters in given string in java., viewed ,<https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/>
VANCOUVER
NJ | Sciencx - » how to find the frequency of characters in given string in java. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/
CHICAGO
" » how to find the frequency of characters in given string in java." NJ | Sciencx - Accessed . https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/
IEEE
" » how to find the frequency of characters in given string in java." NJ | Sciencx [Online]. Available: https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/. [Accessed: ]
rf:citation
» how to find the frequency of characters in given string in java | NJ | Sciencx | https://www.scien.cx/2023/04/27/how-to-find-the-frequency-of-characters-in-given-string-in-java/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.