Delete duplicate rows from two dimentsional array

前端 未结 2 564
渐次进展
渐次进展 2021-01-01 03:06

Let\'s say I have two dimensional array that represents simple matrix

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

2条回答
  •  臣服心动
    2021-01-01 03:46

    int[,] list = new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };            
      List> newList = new List>();
    
       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(list[i,0], list[i,1]);
                        newList.Add(nonDup);
                    }
                }
    

提交回复
热议问题