从两个dimentsional数组中删除重复的行 [英] Delete duplicate rows from two dimentsional array
本文介绍了从两个dimentsional数组中删除重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说我有一个代表简单的矩阵二维数组
INT [,]矩阵=新的INT [,] { {1,2},{3,4},{1,2},{7,8}};
它看起来像
1 2
3 4
1 2
7 8
有没有办法使用LINQ to删除重复的行,并使阵列看起来像这样?
1 2
3 4
7 8
解决方案
这不是真正的LINQ,但如果他们LINQ的方法,你可以定义一些辅助方法
更简单的算法应该是:
- 转换成列表的列表
- 将一种独特的具有自定义比较
- 重建另一个阵列
这看起来是这样的:
公共静态类MyExtensions
{
公共静态的IEnumerable<名单,LT; T>> ToEnumerableOfEnumerable< T>(这件T [,]数组),
{
INT rowCount等= array.GetLength(0);
INT列数= array.GetLength(1);
为(INT的rowIndex = 0;&rowIndex位置LT; rowCount等; rowIndex位置++)
{
无功行=新的List< T>();
为(INT参数:columnIndex = 0;参数:columnIndex<列数;参数:columnIndex ++)
{
row.Add(数组[rowIndex位置,参数:columnIndex]);
}
收益率的回报排;
}
}
公共静态T [,] ToTwoDimensionalArray< T>(名单<名单< T>>的元组)
{
无功名单=元组。了ToList();
T [,]数组= NULL;
为(INT的rowIndex = 0;&rowIndex位置LT; list.Count; rowIndex位置++)
{
无功行=列表[rowIndex位置]
如果(阵列== NULL)
{
阵列=新的T [list.Count,row.Count]
}
为(INT参数:columnIndex = 0;参数:columnIndex< row.Count;参数:columnIndex ++)
{
数组[rowIndex位置,参数:columnIndex] =行[参数:columnIndex]
}
}
返回数组;
}
}
自定义列表比较器的(copied):
公共类ListEqualityComparer< T> :&的IEqualityComparer LT;名单< T>>
{
公共布尔等于(列表< T> X,列表< T> Y)
{
返回x.SequenceEqual(Y);
}
公众诠释的GetHashCode(列表< T> OBJ)
{
INT哈希= 19;
的foreach(OBJ中的变种O)
{
哈希散列= * 31 + o.GetHashCode();
}
返回哈希;
}
}
用法:
[TestClass中]
公共类UnitTest1
{
[TestMethod的]
公共无效TestMethod1()
{
变种数组=新[,] {{1,2},{3,4},{1,2},{7,8}};
阵列= array.ToEnumerableOfEnumerable()
.Distinct(新ListEqualityComparer< INT>())
.ToList()
.ToTwoDimensionalArray();
}
}
Let's say I have two dimensional array that represents simple matrix
int[,] matrix= new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };
It looks like that
1 2
3 4
1 2
7 8
Is there any way to delete duplicate rows using LINQ and make array to look like this?
1 2
3 4
7 8
解决方案
This is not really Linq, but you can define some helper method as if they were Linq methods.
The simpler algorithm should be:
- Convert to a list of list
- Apply a distinct with a custom comparer
- Rebuild another array
This looks like this:
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;
}
}
The custom List comparer (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;
}
}
The usage :
[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();
}
}
这篇关于从两个dimentsional数组中删除重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文