I got a list:
var list = new List<List<int>>();
which could contain
list[0] = {1, 2, 3, 4}
list[1] = {3, 1, 2, 4}
list[2] = {2, 1, 7, 3}
How can I detect the duplicate between [0] and [1] and remove one of them? Code is c-sharp.
In reality it’s not a int, but that shouldn’t change the question.
You could write your own implementation of
IEqualityComparer<List<int>>. ForGetHashCode()it would simply return the XOR of all the hash codes of the elements in the list. ForEquals()it would create a newHashSet<int>from the first list, and callHashSet<T>.SetEqualson 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:As an alternative approach, could you use
HashSet<T>as your collection type to start with? Then it’s really easy:If you need lists as the input but can cope with sets as the output: