繁体   English   中英

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

[英]Given a string as input, return the char which occurs the maximum number of times in the string

您可以假设最多只会出现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;

   }

我正在尝试运行以上代码,但失败了,请问任何建议吗?

尝试这个。

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;
}

为什么不使用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;


 }

将String转换为char [],然后将每个char放入Hashmap中。 对于每个重复出现的字符,它都会增加计数器。 最后返回字符。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM