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

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "my program!";
            Dictionary dic = new Dictionary();
            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 t in dic)
            {
                if (t.Value > dic[max])
                {
                    max = t.Key;
                }
            }
            Console.WriteLine(max + " " + dic[max]);
            Console.ReadLine();
        }
    }
}

你可能感兴趣的:(.NET技术)