从二维数组中删除重复的行

时间:2013-10-25 10:11:26

标签: c# multidimensional-array row

假设我有二维数组代表简单矩阵

int[,] matrix= new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };

看起来像那样

1 2
3 4
1 2
7 8

有没有办法使用LINQ删除重复的行,并使make数组看起来像这样?

1 2
3 4
7 8

2 个答案:

答案 0 :(得分:4)

这不是Linq,但你可以定义一些辅助方法,就像它们是Linq方法一样。

更简单的算法应该是:

  1. 转换为列表
  2. 使用自定义比较器
  3. 应用distinct
  4. 重建另一个数组
  5. 这看起来像这样:

    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;
        }
    }
    

    自定义列表比较器(copied from a Jon Skeet's answer)

    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();
        }
    }
    

答案 1 :(得分: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);
                }
            }