sergtk
sergtk

Reputation: 10974

Subtraction of ordered Lists

How to calculate subtraction (set operation) of ordered lists in C#?

I am interesed in concise solution.

E.g. after execution of code:

List<int> a = new int[] { 1, 2, 5, 6, 7}.ToList();
List<int> b = new int[] { 1, 2, 3, 6}.ToList();
List<int> c = ListSubtract(a, b);

c should contains 5, 7.

It would be good to perform in O(a.Count()+b.Count()) operations, but it is not critical.
Thanks.

Upvotes: 3

Views: 1079

Answers (1)

Oded
Oded

Reputation: 499012

You can use the LINQ Except operator.

List<int> c = a.Except(b).ToList();

Upvotes: 6

Related Questions