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;

        }

  

你可能感兴趣的:(C#)