C#实现对列表(List)中的数据查重操作

发布时间:2021-05-19编辑:佚名阅读(1477)

//查找出列表中的所有重复元素
private static List<string> QueryRepeatElementOfList(List<string> list)
{
    List<string> listTmp = new List<string>();
    if (list!=null && list.Count>0)
    {
        listTmp = list.GroupBy(x => x)
          .Where(g => g.Count() > 1)
          .Select(y => y.Key)
          .ToList();
    }
    return listTmp;
}
//查找出列表中的所有重复元素及其重复次数
private static Dictionary<string, int> QueryRepeatElementAndCountOfList(List<string> list)
{
    Dictionary<string,int> DicTmp = new Dictionary<string, int>();
    if (list != null && list.Count > 0)
    {
        DicTmp = list.GroupBy(x => x)
        .Where(g => g.Count() > 1)
        .ToDictionary(x => x.Key, y => y.Count());
    }
    return DicTmp;
}


  关键字:C#列表List数据查重查找重复操作


鼓掌

1

正能量

0

1

呵呵

0


评论区