- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 print all the common elements of two lists
Firstly create the two lists −
List list1 = new List() {40, 20, 60, 3, 55}; List list2 = new List() {20, 70, 55, 80};
To find the common elements, use the Intersect −
list1.Intersect(list2)
The following is the complete code to find the common elements between the two lists −
Example
using System; using System.Linq; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { List list1 = new List() {40, 20, 60, 3, 55}; List list2 = new List() {20, 70, 55, 80}; Console.WriteLine("Common elements:"); foreach(int value in list1.Intersect(list2)) Console.WriteLine(value); } } }
Output
Common elements: 20 55
- Related Articles
- Python program to print all the common elements of two lists.
- Minimum Index Sum for Common Elements of Two Lists in C++
- C# program to find common values from two or more Lists
- Python program to find common elements in three lists using sets
- C# program to check if two lists have at-least one element common
- Python Program that print elements common at specified index of list elements
- Find common elements in three linked lists in C++
- C# program to print all distinct elements of a given integer array in C#
- C# program to find Intersection of two lists
- Program to print the longest common substring using C++
- Find common elements in list of lists in Python
- Java Program to Find Common Elements in Two ArrayList
- C# program to list the difference between two lists
- Python program to check if two lists have at least one common element
- Program to find minimum difference between two elements from two lists in Python

Advertisements