![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to find Intersection of two lists
To find intersection of two lists in C#, use the Intersect() method.
The following is our list 1.
List<int> list1 = new List<int>(); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(7);
The following is our list 2.
List<int> list2 = new List<int>(); list2.Add(5); list2.Add(4); list2.Add(6); list2.Add(8);
The following is the code to find the intersection of two lists in C#.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Demo { public class Program { public static void Main(String[] args) { List<int> list1 = new List<int>(); list1.Add(2); list1.Add(3); list1.Add(5); list1.Add(7); Console.WriteLine(list1.Count); List<int> list2 = new List<int>(); list2.Add(5); list2.Add(4); list2.Add(6); list2.Add(8); Console.WriteLine(list2.Count); List<int> common = list1.Intersect(list2).ToList(); Console.WriteLine(common.Count); } } }
Output
4 4 1
Advertisements