
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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
- Print the kth common factor of two numbers
- 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
- Python program to find common elements in three lists using sets
- Find common elements in three linked lists 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
- C++ Program for the Common Divisors of Two Numbers?
- Java Program to Find Common Elements in Two ArrayList
- Find count of common nodes in two Doubly Linked Lists in C++
- C# program to list the difference between two lists
Advertisements