How to remove duplicate pairs in a List

  • A+
Category:Languages

I got a List with pairs of integers, How do i remove pairs if theyre duplicates? Distinct wont work cause the pair could be (2, 1) instead of (1, 2).

My list looks like this

1, 2 2, 3 3, 1 3, 2 2, 4 4, 3 

... i dont need (2, 3) and (3, 2) Im kind of new in c# and lost. thanks for any help


You could use a custom IEqualityComparer<FaceLine>:

public class UnorderedFacelineComparer : IEqualityComparer<FaceLine> {     public bool Equals(FaceLine x, FaceLine y)     {         int x1 = Math.Min(x.A, x.B);         int x2 = Math.Max(x.A, x.B);         int y1 = Math.Min(y.A, y.B);         int y2 = Math.Max(y.A, y.B);         return x1 == y1 && x2 == y2;     }      public int GetHashCode(FaceLine obj)     {         return obj.A ^ obj.B;     } } 

Then the query was very simple:

var comparer = new UnorderedFacelineComparer(); List<FaceLine> nonDupList = faceLine     .GroupBy(fl => fl, comparer)     .Where(g => g.Count() == 1)     .Select(g => g.First())     .ToList(); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: