查找重复的字符串数组 [英] Finding Duplicate String Arrays
问题描述
我有一个很大的字符串数组列表,并且在这个List<string[]>
中,可以存在具有所有相同值(并且可能具有不同索引)的数组.我正在寻找并计数这些重复字符串数组,并具有一个Dictionary<string[], int>
,其中int
是计数(但是,如果有比使用字典更好的方法,我想听听一下).有人对如何实现这一目标有任何建议吗?任何和所有输入非常感谢,谢谢!
I have a large list of string arrays, and within this List<string[]>
there can be arrays with all same values (and possibly with different indexes). I'm looking to find and count these duplicate string arrays and have a Dictionary<string[], int>
with int
being the count (however if there is a better way than using a dictionary I would be interested in hearing). Does anyone have any advice on how to achieve this? Any and all input is very appreciated, thanks!
推荐答案
您可以使用linq GroupBy
和IEqualityComparer
来比较string[]
You can use linq GroupBy
with a IEqualityComparer
to compare the string[]
var items = new List<string[]>()
{
new []{"1", "2", "3" ,"4" },
new []{"4","3", "2", "1"},
new []{"1", "2"}
};
var results = items
.GroupBy(i => i, new UnorderedEnumerableComparer<string>())
.ToDictionary(g => g.Key, g => g.Count());
无序列表的IEqualityComparer
public class UnorderedEnumerableComparer<T> : IEqualityComparer<IEnumerable<T>>
{
public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
{
return x.OrderBy(i => i).SequenceEqual(y.OrderBy(i => i));
}
// Just the count of the array,
// it violates the rule of hash code but should be fine here
public int GetHashCode(IEnumerable<T> obj)
{
return obj.Count();
}
}
这篇关于查找重复的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!