List<int> a = 1,2,3
List<int> b = 2,4,5
output
1,3,4,5
views:
208answers:
3
+4
A:
Tried and tested:
List<int> a = new List<int>(){1, 2, 3};
List<int> b = new List<int>(){2, 4, 5};
List<int> c = a.Except(b).Union(b.Except(a)).ToList();
Mitch Wheat
2009-04-03 01:00:13
i want to find the non intersecting part
2009-04-03 01:01:43
I've updated my answer.
Mitch Wheat
2009-04-03 01:11:13
Reed Copsey's answer is the better one!
Mitch Wheat
2009-04-03 01:13:19
+1 for a good answer, despite the 'better' answer.
Mike Christiansen
2009-04-03 01:26:12
+12
A:
The trick is to use Except with the intersection of the two lists.
This should give you the list of non-intersecting elements:
var nonIntersecting = a.Union(b).Except(a.Intersect(b));
Reed Copsey
2009-04-03 01:05:32
A:
Another way :
List<int> a = new List<int> { 1, 2, 3 };
List<int> b = new List<int> { 2, 4, 5 };
var nonIntersecting = a.Union(b)
.Where(x => !a.Contains(x) || !b.Contains(x));
çağdaş
2009-04-03 04:48:39