C# 查处出现次数最多的元素

LINQ

var str= str.ToCharArray()
                       .GroupBy(x => x)
                       .OrderByDescending(x => x.Count())
                       .First()
                       .Key;

算法:

private static string GetChar(string inputString, out int number)
        {
            char[] chars = inputString.ToCharArray();
            number = int.MinValue;
            int originalLength = inputString.Length;
            var cstr = "";
            foreach (var c in chars)
            {
                int len = inputString.Replace(c.ToString(CultureInfo.InvariantCulture),
                                              string.Format("{0}1", c)).Length - originalLength;
                if (len > number)
                {
                    number = len;
                    cstr = c.ToString(CultureInfo.InvariantCulture);
                }
            }
            return cstr;
        }

  

posted @ 2014-05-28 16:03  等待是一生最初的苍老  阅读(572)  评论(0编辑  收藏  举报