Avoid Adding duplicate elements to a List C#

The Solution to Avoid Adding duplicate elements to a List C# is


You can use Enumerable.Except to get distinct items from lines3 which is not in lines2:

lines2.AddRange(lines3.Except(lines2));

If lines2 contains all items from lines3 then nothing will be added. BTW internally Except uses Set<string> to get distinct items from second sequence and to verify those items present in first sequence. So, it's pretty fast.

~ Answered on 2013-01-21 06:34:51


Most Viewed Questions: