在数据表中查找重复行的成本较低的方法
本文关键字:方法 数据表 查找 | 更新日期: 2023-09-27 18:10:48
我想找到DataTable中的所有行,其中每一组列都是重复的。我目前的想法是获取出现一次以上的所有行的索引列表,如下所示:
public List<int> findDuplicates_New()
{
string[] duplicateCheckFields = { "Name", "City" };
List<int> duplicates = new List<int>();
List<string> rowStrs = new List<string>();
string rowStr;
//convert each datarow to a delimited string and add it to list rowStrs
foreach (DataRow dr in submissionsList.Rows)
{
rowStr = string.Empty;
foreach (DataColumn dc in submissionsList.Columns)
{
//only use the duplicateCheckFields in the string
if (duplicateCheckFields.Contains(dc.ColumnName))
{
rowStr += dr[dc].ToString() + "|";
}
}
rowStrs.Add(rowStr);
}
//count how many of each row string are in the list
//add the string's index (which will match the row's index)
//to the duplicates list if more than 1
for (int c = 0; c < rowStrs.Count; c++)
{
if (rowStrs.Count(str => str == rowStrs[c]) > 1)
{
duplicates.Add(c);
}
}
return duplicates;
}
然而,这不是很有效:遍历字符串列表并获得每个字符串的计数需要O(n^2)。我看了这个解决方案,但不知道如何使用它与多个字段。我正在寻找一种更便宜的方法来处理这个问题。
试试这个:
如何检查表中每行有70+列的精确匹配?
本质是创建一个集合,其中存储行的哈希值,并且只在具有冲突哈希值的行之间进行比较,复杂度将为O(n)
…
如果你有一个大行和存储哈希本身是一个问题(不太可能的情况下,但仍然…),你可以使用Bloom过滤器。Bloom过滤器的核心思想是计算每行的几个不同的哈希值,并将它们用作位图中的地址。当你扫描行时,你可以仔细检查那些已经包含了之前设置的位图中的所有位的行。