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 check if two lists have at-least one element common
A common requirement in C# programming is to check if two lists share at least one common element. This can be achieved using various approaches, from simple nested loops to more efficient methods using HashSets and LINQ.
The most efficient approach is to use HashSet operations or LINQ's Intersect() method, which provide optimal performance for finding common elements between collections.
Using HashSet Intersection
HashSet provides an Overlaps() method that efficiently checks if two sets have any common elements −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
int[] arr1 = { 65, 57, 63, 98 };
int[] arr2 = { 43, 65, 33, 57 };
var set1 = new HashSet<int>(arr1);
var set2 = new HashSet<int>(arr2);
Console.WriteLine("List 1: " + string.Join(", ", arr1));
Console.WriteLine("List 2: " + string.Join(", ", arr2));
bool hasCommon = set1.Overlaps(set2);
Console.WriteLine("Have common elements: " + hasCommon);
if (hasCommon) {
set1.IntersectWith(set2);
Console.WriteLine("Common elements: " + string.Join(", ", set1));
}
}
}
The output of the above code is −
List 1: 65, 57, 63, 98 List 2: 43, 65, 33, 57 Have common elements: True Common elements: 65, 57
Using LINQ Intersect Method
LINQ provides the Intersect() method to find common elements, and Any() to check if the intersection has any elements −
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] list1 = { 10, 20, 30, 40 };
int[] list2 = { 25, 30, 35, 45 };
Console.WriteLine("List 1: " + string.Join(", ", list1));
Console.WriteLine("List 2: " + string.Join(", ", list2));
var commonElements = list1.Intersect(list2);
bool hasCommon = commonElements.Any();
Console.WriteLine("Have common elements: " + hasCommon);
if (hasCommon) {
Console.WriteLine("Common elements: " + string.Join(", ", commonElements));
}
}
}
The output of the above code is −
List 1: 10, 20, 30, 40 List 2: 25, 30, 35, 45 Have common elements: True Common elements: 30
Using Nested Loop Approach
For simple scenarios or when LINQ is not available, a nested loop can check for common elements −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
int[] list1 = { 1, 2, 3, 4 };
int[] list2 = { 5, 6, 7, 8 };
Console.WriteLine("List 1: " + string.Join(", ", list1));
Console.WriteLine("List 2: " + string.Join(", ", list2));
bool hasCommon = false;
var common = new List<int>();
for (int i = 0; i < list1.Length; i++) {
for (int j = 0; j < list2.Length; j++) {
if (list1[i] == list2[j] && !common.Contains(list1[i])) {
hasCommon = true;
common.Add(list1[i]);
}
}
}
Console.WriteLine("Have common elements: " + hasCommon);
if (hasCommon) {
Console.WriteLine("Common elements: " + string.Join(", ", common));
}
}
}
The output of the above code is −
List 1: 1, 2, 3, 4 List 2: 5, 6, 7, 8 Have common elements: False
Performance Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| HashSet.Overlaps() | O(min(m,n)) | Large collections, best performance |
| LINQ Intersect() | O(m+n) | Readable code, moderate performance |
| Nested Loops | O(m×n) | Simple scenarios, small collections |
Conclusion
The most efficient way to check if two lists have common elements is using HashSet's Overlaps() method, which offers O(min(m,n)) performance. For readable code with good performance, LINQ's Intersect() method is also an excellent choice.
