static void Main(string[] args)
{
string str = "abcabcqwe";
if (str.Length > 0)
{
//把字符串进行分割
char[] strChar = str.ToCharArray();
//把字符先分组后排序
var c = strChar.GroupBy(m => m).OrderByDescending(m => m.Count()).ToList();
var first = strChar.GroupBy(m => m).OrderByDescending(m => m.Count()).First().ToList();//第一位
var last = strChar.GroupBy(m => m).OrderByDescending(m => m.Count()).Last().ToList();//最后一位
List<char> charList = new List<char>();
for (int i = 0; i < c.Count; i++)
{
//得到可能相同的字符
if (first.Count == c[i].Count())
{
charList.Add(c[i].Key);
}
}
//显示字符
string charItem = "";
foreach (var item in charList)
{
charItem += item + ",";
}
Console.WriteLine("{0}是出现最多的字符串 共:{1}次!", charItem, first.Count.ToString());
}
Console.ReadLine();
}
这只是一种做法