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 elements in three arrays using sets
In C#, you can find common elements in three arrays efficiently using HashSet collections. A HashSet provides fast lookups and set operations, making it ideal for finding intersections between collections of data.
The HashSet<T> class offers built-in methods like IntersectWith() that can perform set operations directly, eliminating the need for complex loops and comparisons.
Syntax
Following is the syntax for creating a HashSet from an array −
var hashSet = new HashSet<int>(array);
Following is the syntax for finding intersection using IntersectWith() −
hashSet1.IntersectWith(hashSet2); hashSet1.IntersectWith(hashSet3);
Using HashSet IntersectWith Method
The most efficient approach uses the IntersectWith() method to find common elements −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
int[] arr1 = { 99, 57, 63, 98 };
int[] arr2 = { 43, 99, 33, 57 };
int[] arr3 = { 99, 57, 42 };
// Create HashSets from arrays
var h1 = new HashSet<int>(arr1);
var h2 = new HashSet<int>(arr2);
var h3 = new HashSet<int>(arr3);
Console.WriteLine("Array 1: " + string.Join(", ", arr1));
Console.WriteLine("Array 2: " + string.Join(", ", arr2));
Console.WriteLine("Array 3: " + string.Join(", ", arr3));
// Find intersection of all three sets
h1.IntersectWith(h2);
h1.IntersectWith(h3);
Console.WriteLine("Common elements: " + string.Join(", ", h1));
}
}
The output of the above code is −
Array 1: 99, 57, 63, 98 Array 2: 43, 99, 33, 57 Array 3: 99, 57, 42 Common elements: 99, 57
Using LINQ Intersect Method
Another approach uses LINQ's Intersect() method for a more functional programming style −
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] arr1 = { 99, 57, 63, 98 };
int[] arr2 = { 43, 99, 33, 57 };
int[] arr3 = { 99, 57, 42 };
Console.WriteLine("Array 1: " + string.Join(", ", arr1));
Console.WriteLine("Array 2: " + string.Join(", ", arr2));
Console.WriteLine("Array 3: " + string.Join(", ", arr3));
// Find common elements using LINQ
var commonElements = arr1.Intersect(arr2).Intersect(arr3);
Console.WriteLine("Common elements: " + string.Join(", ", commonElements));
}
}
The output of the above code is −
Array 1: 99, 57, 63, 98 Array 2: 43, 99, 33, 57 Array 3: 99, 57, 42 Common elements: 99, 57
Comparison of Approaches
| Method | Performance | Readability | Memory Usage |
|---|---|---|---|
| HashSet.IntersectWith() | O(n) - Fast | Good | Moderate |
| LINQ.Intersect() | O(n) - Fast | Excellent | Higher (creates intermediate collections) |
| Manual Loop Comparison | O(n²) - Slower | Poor | Low |
Conclusion
Finding common elements in three arrays using HashSet collections is efficient and straightforward in C#. The HashSet.IntersectWith() method provides optimal performance, while LINQ's Intersect() offers excellent readability for functional programming approaches.
