从二维数组中删除重复行

5
假设我有一个二维数组,代表一个简单的矩阵。
int[,] matrix= new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };

看起来像是这样。
1 2
3 4
1 2
7 8

有没有办法使用LINQ删除重复行并使数组看起来像这样?
1 2
3 4
7 8

数组是静态大小的。您无法删除项目(但可以创建新数组)。 - SJuan76
2个回答

6

虽然不是真正的Linq,但您可以定义一些帮助方法,好像它们是Linq方法。

更简单的算法应该是:

  1. 将其转换为一个列表
  2. 应用具有自定义比较器的distinct方法
  3. 重新构建另一个数组

如下所示:

public static class MyExtensions
{
    public static IEnumerable<List<T>> ToEnumerableOfEnumerable<T>(this T[,] array)
    {
        int rowCount = array.GetLength(0);
        int columnCount = array.GetLength(1);

        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
        {
            var row = new List<T>();
            for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
            {
                row.Add(array[rowIndex, columnIndex]);
            }
            yield return row;
        }
    }
    public static T[,] ToTwoDimensionalArray<T>(this List<List<T>> tuples)
    {
        var list = tuples.ToList();
        T[,] array = null;
        for (int rowIndex = 0; rowIndex < list.Count; rowIndex++)
        {
            var row = list[rowIndex];
            if (array == null)
            {
                array = new T[list.Count, row.Count];
            }
            for (int columnIndex = 0; columnIndex < row.Count; columnIndex++)
            {
                array[rowIndex, columnIndex] = row[columnIndex];
            }
        }
        return array;
    }
}

自定义列表比较器 (摘自 Jon Skeet 的回答):

public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
    public bool Equals(List<T> x, List<T> y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(List<T> obj)
    {
        int hash = 19;
        foreach (var o in obj)
        {
            hash = hash * 31 + o.GetHashCode();
        }
        return hash;
    }
}

使用方法:
[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var array = new[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };
        array = array.ToEnumerableOfEnumerable()
                     .Distinct(new ListEqualityComparer<int>())
                     .ToList()
                     .ToTwoDimensionalArray();
    }
}

如果想使用LINQ,您需要使用锯齿数组,在其中更容易地过滤不同的元素。因此,必须将其转换为锯齿数组。+1 - Carbine
这个就像魔法一样好用!非常感谢你的帮助。 - Mitya

0
int[,] list = new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };            
  List<KeyValuePair<Int32, Int32>> newList = new List<KeyValuePair<int,int>>();

   bool dupFound;

       for (int i = 0; i < list.Length; i++)
       {
         dupFound = false;

         for (int a = 0; a < list.Length; i++)
          {
           if ((i != a) && list[a, 0] == list[i, 0] && list[a, 1] == list[i, 1])
           {
              dupFound = true;
             break;
                   }
            }

               if (!dupFound)
                {
                    var nonDup = new KeyValuePair<Int32, Int32>(list[i,0], list[i,1]);
                    newList.Add(nonDup);
                }
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接