Program to find the frequency of characters in given string in java
For Explanation Watch Video :
Code::
import java.util.*;
import java.io.*;
import java.lang.*;
class Test{
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:
{d=1, t=1, i=2, o=1}
d 1
t 1
i 2
o 1
Comments
Post a Comment