华为上机试题(java)找出一个字符串中出现次数最多的字符

 Java求字符串中出现次数最多的字符,如String Str = "aaabbcddddee";那么输出:d 4 ;若String Str = "aaabbcddddeexxxxxx";那么输出:x 6
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;

public class Maxkey
{
    public static void main(String[] args)
    {

        String str ="aabbbbcc";
        char[] StrArr = str.toCharArray();
        Map map = MapFunction(StrArr);
        char ch = FindMapMaxValue(map);
    }

        public static Map MapFunction(char[] StrArr)
        {
            Map map = new HashMap();
            if(!(StrArr == null || StrArr.length == 0))
            {
                for(int i = 0;i map)
        {
            Set keys = map.keySet();
            Iterator keys_Itera = keys.iterator();
            Character maxkey = (Character)keys_Itera.next();
            int maxvalue = map.get(maxkey);

            while(keys_Itera.hasNext())
            {
                Character temp = (Character)keys_Itera.next();
                if(maxvalue: "+maxkey+"  "+
            "maxvalue is ---> :"+maxvalue);
            return maxkey;
        } 
    }

结果:

maxkey is --->: b  maxvalue is ---> :4



你可能感兴趣的:(java,面试)