I have a List
of my class Edge
:
public class Edge
{
public Vector3 a;
public Vector3 b;
public Edge(Vector3 a, Vector3 b)
{
this.a = a;
this.b = b;
}
}
I want ALL my duplicates to be removed from my list, an element is considered duplicated if
(edge1.a == edge2.a && edge1.b == edge2.b)
or
(edge1.a == edge2.b && edge1.b == edge2.a)
I've tried to use the Disctinct()
method with an equality comparer and some other LINQ variants, they work as they are supposed to, but that is not the result I want. All the solutions I've found always leave one of the duplicate elements inside the list, when I need every duplicate to be removed from it.
public class SomeClass: MonoBehaviour
{
[SerializeField] private List<Edge> edges;
private void Start()
{
edges = edges
.Distinct(new EdgeComparer())
.ToList();
// This removes the duplicates except for one,
// I need that one to be removed as well.
}
}
public class EdgeComparer : IEqualityComparer<Edge>
{
public bool Equals(Edge x, Edge y)
{
return
(x.a.Equals(y.a) && x.b.Equals(y.b)) ||
(x.a.Equals(y.b) && x.b.Equals(y.a));
}
public int GetHashCode(Edge obj)
{
int a = obj.a.GetHashCode();
int b = obj.b.GetHashCode();
return a ^ b;
}
}