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

前端 未结 30 2496
遇见更好的自我
遇见更好的自我 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:40

    public static void main(String[] args) {
            String name="AnuvratAnuvra";
            char[] arr = name.toCharArray();
            HashMap map = new HashMap();
    
            for(char val:arr){          
                map.put(val,map.containsKey(val)?map.get(val)+1:1);         
            }
    
            for (Entry entry : map.entrySet()) {
                if(entry.getValue()>1){
                Character key = entry.getKey();
                Object value = entry.getValue();
    
                System.out.println(key + ":"+value);
                }
                }
        }
    

提交回复
热议问题