【问题标题】:How to remove duplicate list in List<List<int>>如何删除 List<List<int>> 中的重复列表
【发布时间】:2015-09-22 14:42:20
【问题描述】:

我有List&lt;List&lt;int&gt;&gt;,例如 List&lt;List&lt;int&gt;&gt;{{1,2,3}, {1,1,2,}, {1,2,3}}

我想删除其中的重复项: 结果应该是:{{1,2,3}, {1,1,2}}

问题是内部列表是引用类型,因此它们具有不同的对象哈希码,因此被分开处理。

我不想完全迭代列表来查找重复项,因为它不是最佳的。

【问题讨论】:

  • 我没听懂最后一句话。最后,您只需查看每个元素(无论是“隐藏”在某些 LINQ 代码后面还是显式)。

标签: c# .net linq equality


【解决方案1】:

试试这个:

List<List<int>> lst = new List<List<int>>()
{
    new List<int> {1,2,3},
    new List<int> {1,1,2}, 
    new List<int> {1,2,3}
};

var result = lst.GroupBy(c => String.Join(",", c)).Select(c => c.First().ToList()).ToList();

【讨论】:

    【解决方案2】:

    您可以实现一个EqualityComparer 类并在LINQ 的Distinct 方法中使用它。

    public class CustomEqualityComparer : IEqualityComparer<List<int>>
    {
         public bool Equals(List<int> x, List<int> y)
         {
             if (x.Count != y.Count)
                 return false;
             for (int i = 0; i < x.Count; i++)
             {
                if (x[i] != y[i])
                    return false;
             }
             return true;
         }
    
         public int GetHashCode(List<int> obj)
         {
             return 0;
         }
    }
    

    并像这样使用它

    public static void Main(string[] args)
    {
        var list = new List<List<int>>() { new List<int> { 1, 1, 2 }, new List<int> { 1, 2, 3 }, new List<int> { 1, 1, 2 } };
        var res = list.Distinct(new CustomEqualityComparer());
        Console.WriteLine("Press any key to continue.");
        Console.ReadLine();
    }
    

    【讨论】:

    • 如果你不知道集合的数量,即它是否总是很小,那么你应该实现GetHashCode。它被Distinct 方法使用。
    【解决方案3】:

    很简单:

    List<List<int>> lst = new List<List<int>>()
    {
        new List<int> {1,2,3},
        new List<int> {1,1,2,}, 
        new List<int> {1,2,3},
    };
    
    var result =
        lst
            .Where((xs, n) =>
                !lst
                    .Skip(n + 1)
                    .Any(ys => xs.SequenceEqual(ys)))
            .ToList();
    

    我得到这个结果:

    【讨论】:

      【解决方案4】:

      你想要的比简单的比较更复杂。 在我看来,您应该创建一个新的类型/类,例如 整数集合:ICollection

      那么你应该以这种方式实现 Equals:

      bool Equals(IntegerCollection col) {    if(this.Count() != col.Count())
            return false;
      
         if(this.Sum() != col.Sum())
            return false;
      
         for(int i = 0; i < this.Count(); i++)    {
            if(this[i]==col[i]){
               continue;
            }else{
               return false;
            }    }
      
         return true; }
      

      最后

      List<IntegerCollection> collections = new List<IntegerCollection> {
           new IntegerCollection({1,2,3}),
           new IntegerCollection({1,1,2}),
           new IntegerCollection({1,2,3})};
      var distincts = collections.Distinct();
      

      【讨论】:

      • 一些建议是在 F# 中你有这个选项 navite 基于 tuplas
      【解决方案5】:

      使用@faridCustomEqualityComparer也可以使用HashSet

      List<List<int>> RemoveDuplicates(IEnumerable<List<int>> values)
      {
          return new HashSet<List<int>>(values,new CustomEqualityComparer()).ToList();
      }
      

      【讨论】:

        猜你喜欢
        • 2016-07-02
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-29
        • 2012-06-07
        • 1970-01-01
        • 2019-07-28
        相关资源
        最近更新 更多