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);
}
}
}
------解决方案-------------------- 用正则
------解决方案-------------------- string str = "abcdacbdeidaadbefsadaac"; char[] ary = str.ToCharArray(); ArrayList al = new ArrayList(); foreach (char c in ary) { if(!al.Contains(c)) { Regex reg = new Regex(c.ToString()); al.Add(reg.Matches(str).Count.ToString()+"|"+c.ToString()); } } al.Sort(); Response.Write(al[al.Count - 1].ToString());
------解决方案-------------------- char c = str.ToCharArray().GroupBy(ch => ch).OrderBy( g => -g.Count() ).First().Key;
------解决方案-------------------- linq:
var resultGroup = from aChar in str.ToCharArray() group aChar by aChar into g orderby g.Count() descending select g ;
------解决方案--------------------
------解决方案--------------------
C# code
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());
}
------解决方案--------------------