List<string>里 每个元素重复了多少次
static void Main(string[] args)
{
List<string> list = new List<string>();
list.Add("aaa");
list.Add("aa");
list.Add("dd");
list.Add("aaa");
list.Add("dd");
list.Add("aa");
list.Add("aaa");
int a = RepeatTimes(list, "aaa");//某个对象重复有多少次
//打印出重复多少次
foreach (var item in list.GroupBy(s => s))
{
Console.WriteLine("{0}:{1}次", item.Key, item.Count());
}
}
/// <summary>
/// 某个元素重复次数
/// </summary>
/// <param name="L"></param>
/// <param name="text"></param>
/// <returns></returns>
private static int RepeatTimes(List<string> list, string text)
{
int num = 0;
foreach (string str in list)
if (str == text)
num++;
return num;
}