An Interest In:
Web News this Week
- April 3, 2024
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
April 27, 2023 02:54 am GMT
Original Link: https://dev.to/realnamehidden1_61/program-to-find-the-frequency-of-characters-in-given-string-in-java-100o
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.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:
Original Link: https://dev.to/realnamehidden1_61/program-to-find-the-frequency-of-characters-in-given-string-in-java-100o
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To