您的位置:首页 > 职场人生

c#面试题:找出字符串中出现次数最多的字符及出现次数

2013-03-22 09:22 344 查看
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "my program!";
            Dictionary<char, int> dic = new Dictionary<char, int>();
            for (int i = 0; i < str.Length; i++)
            {
                if (dic.ContainsKey(str[i]))
                    dic[str[i]]++;
                else
                    dic[str[i]] = 1;
            }
            char max = str[0];
            foreach (KeyValuePair<char, int> t in dic)
            {
                if (t.Value > dic[max])
                {
                    max = t.Key;
                }
            }
            Console.WriteLine(max + " " + dic[max]);
            Console.ReadLine();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐