You could write your own implementation of IEqualityComparer<List<int>>
. For GetHashCode()
it would simply return the XOR of all the hash codes of the elements in the list. For Equals()
it would create a new HashSet<int>
from the first list, and call HashSet<T>.SetEquals
on it, passing in the second list. This assumes there will be no duplicate elements, mind you. (Otherwise { 1, 1, 2 } will be equal to { 1, 2, 2 } but have a different hash code.)
Once you've got that far, you can use Distinct
:
var distinct = list.Distinct(new CustomEqualityComparer());
As an alternative approach, could you use HashSet<T>
as your collection type to start with? Then it's really easy:
var distinct = sets.Distinct(HashSet<int>.CreateSetComparer());
If you need lists as the input but can cope with sets as the output:
var distinct = list.Select(x => new HashSet<int>(x))
.Distinct(HashSet<int>.CreateSetComparer());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…