Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to find common values from two or more Lists
Finding common values from two or more Lists in C# is a frequent requirement in programming. The Intersect() method from LINQ provides an efficient way to identify elements that exist in multiple collections.
Syntax
Following is the syntax for using Intersect() method −
var result = list1.Intersect(list2);
For multiple lists, chain the Intersect() method −
var result = list1.Intersect(list2).Intersect(list3);
Using Intersect() with Two Lists
The Intersect() method returns elements that appear in both lists. It automatically removes duplicates and maintains the order from the first list −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
// two lists
var list1 = new List<int>{3, 4, 5, 6};
var list2 = new List<int>{1, 2, 3, 5, 7};
// common values
var res = list1.Intersect(list2);
Console.WriteLine("Common values:");
foreach(int i in res) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Common values: 3 5
Using Intersect() with Multiple Lists
You can find common elements across multiple lists by chaining the Intersect() method −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
// three lists
var list1 = new List<int>{1, 2, 3, 4, 5};
var list2 = new List<int>{3, 4, 5, 6, 7};
var list3 = new List<int>{4, 5, 6, 7, 8};
// common values across all three lists
var res = list1.Intersect(list2).Intersect(list3);
Console.WriteLine("Common values in all three lists:");
foreach(int i in res) {
Console.WriteLine(i);
}
}
}
The output of the above code is −
Common values in all three lists: 4 5
Using Intersect() with String Lists
The Intersect() method works with any data type, including strings −
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
// string lists
var fruits1 = new List<string>{"apple", "banana", "orange", "grape"};
var fruits2 = new List<string>{"banana", "grape", "mango", "kiwi"};
// common fruits
var commonFruits = fruits1.Intersect(fruits2);
Console.WriteLine("Common fruits:");
foreach(string fruit in commonFruits) {
Console.WriteLine(fruit);
}
}
}
The output of the above code is −
Common fruits: banana grape
Key Features of Intersect()
-
Removes duplicates: Returns only unique elements from the intersection.
-
Maintains order: Preserves the order of elements from the first list.
-
Lazy evaluation: Uses deferred execution − results are computed when enumerated.
-
Type-safe: Works with any data type that implements equality comparison.
Conclusion
The Intersect() method in C# provides an efficient way to find common elements between lists. It automatically handles duplicates, maintains order from the first list, and can be chained for multiple list comparisons, making it ideal for set operations in LINQ.
