I’m having a List of String like
List<string> MyList = new List<string>
{
"A-B",
"B-A",
"C-D",
"C-E",
"D-C",
"D-E",
"E-C",
"E-D",
"F-G",
"G-F"
};
I need to remove duplicate from the List i.e, if “A-B” and “B-A” exist then i need to keep only “A-B” (First entry)
So the result will be like
"A-B"
"C-D"
"C-E"
"D-E"
"F-G"
Is there any way to do this using LINQ?
This returns the sequence you look for:
In short: split each string on the
-character into two-element arrays. Sort each array, and join them back together. Then you can simply useDistinctto get the unique values.Update: when thinking a bit more, I realized that you can easily remove one of the
Selectcalls:Disclaimer: this solution will always keep the value “A-B” over “B-A”, regardless of the order in which the appear in the original sequence.