Find duplicate characters in a String and count the number of occurances using Java

前端 未结 30 2483
遇见更好的自我
遇见更好的自我 2020-12-14 11:47

How can I find the number of occurrences of a character in a string?

For example: The quick brown fox jumped over the lazy dog.

Some example

30条回答
  •  既然无缘
    2020-12-14 12:51

    import java.util.HashMap;
    import java.util.Scanner;
    
    
    public class HashMapDemo {
    
        public static void main(String[] args) {
            //Create HashMap object to Store Element as Key and Value 
            HashMap hm= new HashMap();
            //Enter Your String From Console
            System.out.println("Enter an String:");
            //Create Scanner Class Object From Retrive the element from console to our java application
            Scanner sc = new Scanner(System.in);
            //Store Data in an string format
            String s1=sc.nextLine();
            //find the length of an string and check that hashmap object contain the character or not by using 
            //containskey() if that map object contain element only one than count that value as one or if it contain more than one than increment value 
    
            for(int i=0;i

提交回复
热议问题