显示char []数组中出现的每个char多于指定的char // Java

时间:2023-01-05 23:35:31

I am trying to write a method in Java that will take (char[] abc,char x) as parameters.

我正在尝试用Java编写一个方法(char [] abc,char x)作为参数。

It should :

这应该 :

a)check whether the specified character appears in the array and how many times it appears in the array. b)Display each character that appears in a given array of chars for more than the specified number of characters.

a)检查指定的字符是否出现在数组中以及它出现在数组中的次数。 b)显示给定字符数组中出现的每个字符超过指定数量的字符。

Example for method parameters

方法参数的示例

char[] abc={'a','a','a','a','b','b','b','c','c','d','x'};

char x='x';

All help & tips are welcome !

欢迎提供所有帮助和提示!

1 个解决方案

#1


1  

This might help

这可能有所帮助

public static void countChar(char[] abc, char x){
    Map<Character, Integer> map = new HashMap<>();

    for(char c: abc){
        if(map.containsKey(c))
            map.put(c,map.get(c)+1);
        else
            map.put(c,1);
    }

    if(map.get(x)!=null){
        int count = map.get(x);
        System.out.println(x + " exists " + count + " times in char array!");
        for(char c: map.keySet()){
            if(map.get(c)>count)
                System.out.println(c);
        }
    }else{
        System.out.println(x + " does not exist in char array!");
    }
}

#1


1  

This might help

这可能有所帮助

public static void countChar(char[] abc, char x){
    Map<Character, Integer> map = new HashMap<>();

    for(char c: abc){
        if(map.containsKey(c))
            map.put(c,map.get(c)+1);
        else
            map.put(c,1);
    }

    if(map.get(x)!=null){
        int count = map.get(x);
        System.out.println(x + " exists " + count + " times in char array!");
        for(char c: map.keySet()){
            if(map.get(c)>count)
                System.out.println(c);
        }
    }else{
        System.out.println(x + " does not exist in char array!");
    }
}