给定一个字符串作为输入,返回在字符串中出现最大次数的char

时间:2015-08-31 20:29:30

标签: java

您可以假设只有1个字符出现最多次数。

         Input             |     Output  
---------------------------+--------------
mostFreq("hello")          |       l
mostFreq("1223334445555")  |       5
mostFreq("z")              |       z
public char mostFreq(String str){

      int count=0;
        String temp=""; // An empty string to keep track of counted
                       // characters
        for(int i=0;i<str.length();i++)
        {

            char c=str.charAt(i);  // take one character (c) in string

            for(int j=i;j<str.length();j++)
            {
                char k=str.charAt(j);  
            // take one character (c) and compare with each character (k) in the string
            // also check that character (c) is not already counted.
            // if condition passes then increment the count.
                if(c==k && temp.indexOf(c)==-1)                                                                          
                {

                    count=count+1;

                }
             }

             if(temp.indexOf(c)==-1)  // if it is not already counted
             {    

            temp=temp+c; // append the character to the temp indicating
                        // that you have already counted it
             } 
             return c;
        }
        return 0;

   }

我正在尝试运行上面的代码,但它失败了,有什么建议吗?

3 个答案:

答案 0 :(得分:1)

试试这个。

public char mostFreq(String str){

    int highestFreq = 0;

      char mostFreqChar = ' ';

      for (int i = 0; i < str.length(); i++)

      {

          //Get a char and go through entire string to determine how many times that char occurs

          char x = str.charAt(i);
          int c = 0;

          for (int j = str.indexOf(x); j != -1; j = str.indexOf(x, j + 1))

          {

              c++;

          }

          if (c > highestFreq)

          {

              highestFreq = c;

              mostFreqChar = x;
          }

      }

      return mostFreqChar;
}

答案 1 :(得分:0)

为什么不使用Map并索引每个角色的出现?我们可以这样做:

 public int mostFreq(String str){
     Map<Character, Integer> occurences = new HashMap<>();
     char[] characters = str.toCharArray();

     for (int i = 0; i < characters.length; i++) {
         if (occurences.get(characters[i]) == null)
             occurences.put(characters[i], 1)

         else {
             int amount = occurences.get(characters[i]);
             amount++;
             occurences.put(characters[i], amount);
         }
     }

     int max = 0;
     Iterator<Integer> iterator = occurences.keyset().iterator();

     while (iterator.hasNext()) {
         int next = iterator.next();

         if (next > max)
            max = next;


     }

     return next;


 }

答案 2 :(得分:0)

将String转换为char [],然后将每个char放在Hashmap中。对于每个reoccuring char,它会增加计数器。最后返回char。

public char mostFreq(String str){
    Map<Character,Integer> occurences = new HashMap<Character, Integer>();
    Character mostFreq = null ;
    if (str !=null && str.length()>0){
        char[] chars = str.toCharArray();
        for(char c : chars){
            // increase occurences if exists
            if (occurences.containsKey(c)){
                occurences.put(c, occurences.get(c)+1);
            // create an entry if not
            }else{
                occurences.put(c, 1);
            }
        }
        // search for key with highest value
        int count = 0;
        for(Map.Entry<Character,Integer> entry : occurences.entrySet()){
            if (entry.getValue() > count){
                mostFreq = entry.getKey();
                count = entry.getValue();
            }
        }
    }
    return mostFreq;
}