日期:2014-05-20 浏览次数:20957 次
public static void ThirdWay(string str)
{
var resultGroup = from aChar in str.ToCharArray()
group aChar by aChar;
int max = 0;
foreach (var one in resultGroup)
{
if (one.Count() > 0)
{
max = one.Count();
}
}
foreach (var one in resultGroup)
{
if (one.Count() == max)
{
Console.WriteLine("{0}字符出现了{1}次", one.Key, max);
}
}
}
public static void ThirdWay(string str)
{
var group = from c in str group c by c into g orderby g.Count() descending select g;
Console.WriteLine("{0}字符出现了{1}次",group.First().Key,group.First().Count());
}
------解决方案--------------------
public static void MostChar(string input)
{
int maxCount = 0; char maxChar = '\0';
while (input.Length > 0)
{
char tempChar = input[0];
int count = input.Length
- (input = input.Replace(input[0].ToString(), string.Empty)).Length;
if (count > maxCount)
{
maxCount = count;
maxChar = tempChar;
}
}
Console.WriteLine("{0}, {1}", maxChar, maxCount);
}