Let’s say I have these two arrays:
var array1 = new[] {"A", "B", "C"}; var array2 = new[] {"A", "C", "D"};
I would like to get the differences between the two. I know I could write this in just a few lines of code, but I want to make sure I’m not missing a built in language feature or a LINQ extension method.
Ideally, I would end up with the following three results:
- Items not in array1, but are in array2 ("D")
- Items not in array2, but are in array1 ("B")
- Items that are in both
If you’ve got LINQ available to you, you can use
ExceptandDistinct. The sets you asked for in the question are respectively: