static void Main(string[] args)
{
string[] strArr = { "aa", "bb", "cc", "dd", "aa", "bb", "aa", "aa", "bb", "cc" ,"dd", "bb", "dd", "dd", "dd", "cc", "cc", "cc", "cc", "cc" };
HashSet<string> strTemp = new HashSet<string>();
foreach (var item in strArr)
{
strTemp.Add(item);
}
Dictionary<string, int> strCount = new Dictionary<string, int>();
string[] strT = strTemp.ToArray<string>();
for (int i = 0; i < strT.Length; i++)
{
int k = 0;
for (int j = 0; j < strArr.Length; j++)
{
if (strT[i] == strArr[j])
{
k++;
}
}
strCount.Add(strArr[i], k);
}
List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(strCount);
lst.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
{
return s2.Value.CompareTo(s1.Value);
});
foreach (var item in lst)
{
Console.WriteLine(item.Key+" "+item.Value);
}
Console.ReadKey();
}
字符串统计个数并排序
最新推荐文章于 2021-11-09 10:50:22 发布