Find set difference between two lists in C#
This post will discuss how to find the set difference between the two lists in C#.
The solution should compare two lists, and return those in the first list but not in the second list and vice versa. We can use LINQ query operations to solve this in C# easily.
1. Using Except() method
The recommended solution is to use LINQ’s Except() method to find the elements of a sequence that don’t appear in the other sequence. This can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> first = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; List<int> second = new List<int>() { 6, 7, 8, 9 }; IEnumerable<int> firstDiffSecond = first.Except(second); Console.WriteLine("X-Y : " + String.Join(',', firstDiffSecond)); IEnumerable<int> secondDiffFirst = second.Except(first); Console.WriteLine("Y-X : " + String.Join(',', secondDiffFirst)); } } /* Output: X-Y : 1,2,3,4,5 Y-X : 8,9 */ |
2. Using Where() with Any() method
The following code example demonstrates how to use the Where() method in combination with the Any() method to find the elements of a sequence that don’t appear in the other sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> first = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; List<int> second = new List<int>() { 6, 7, 8, 9 }; IEnumerable<int> firstDiffSecond = first.Where(item => !second.Any(e => item == e)); Console.WriteLine("X-Y : " + String.Join(',', firstDiffSecond)); IEnumerable<int> secondDiffFirst = second.Where(item => !first.Any(e => item == e)); Console.WriteLine("Y-X : " + String.Join(',', secondDiffFirst)); } } /* Output: X-Y : 1,2,3,4,5 Y-X : 8,9 */ |
3. Using Where() with Contains() method
The following code example demonstrates how to use the Where() method in combination with the Contains() method to find the elements of a sequence that don’t appear in the other sequence.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> first = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; List<int> second = new List<int>() { 6, 7, 8, 9 }; IEnumerable<int> firstDiffSecond = first.Where(item => !second.Contains(item)); Console.WriteLine("X-Y : " + String.Join(',', firstDiffSecond)); IEnumerable<int> secondDiffFirst = second.Where(item => !first.Contains(item)); Console.WriteLine("Y-X : " + String.Join(',', secondDiffFirst)); } } /* Output: X-Y : 1,2,3,4,5 Y-X : 8,9 */ |
That’s all about finding the set difference between two lists in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)