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
Check if a HashSet and a specified collection share common element in C#
To check if a HashSet and a specified collection share a common element, we use the Overlaps() method in C#. This method returns true if the HashSet shares at least one element with the specified collection, and false otherwise.
Syntax
Following is the syntax for the Overlaps() method −
public bool Overlaps(IEnumerable<T> other)
Parameters
other − The collection to compare with the current HashSet.
Return Value
Returns true if the HashSet and the specified collection share at least one common element; otherwise, false.
Using Overlaps() with Integer HashSets
The following example demonstrates checking for common elements between two integer HashSets −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<int> set1 = new HashSet<int>();
set1.Add(25);
set1.Add(50);
set1.Add(75);
set1.Add(100);
set1.Add(125);
set1.Add(150);
Console.WriteLine("Elements in HashSet1");
foreach(int val in set1){
Console.WriteLine(val);
}
HashSet<int> set2 = new HashSet<int>();
set2.Add(30);
set2.Add(60);
set2.Add(100);
set2.Add(150);
set2.Add(200);
set2.Add(250);
Console.WriteLine("Elements in HashSet2");
foreach(int val in set2){
Console.WriteLine(val);
}
Console.WriteLine("Do they share common elements? " + set1.Overlaps(set2));
}
}
The output of the above code is −
Elements in HashSet1 25 50 75 100 125 150 Elements in HashSet2 30 60 100 150 200 250 Do they share common elements? True
Using Overlaps() with String HashSets
The following example shows how to check for overlapping elements in string collections −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<String> set1 = new HashSet<String>();
set1.Add("Nathan");
set1.Add("Tim");
set1.Add("Tom");
set1.Add("Jack");
set1.Add("Steve");
set1.Add("David");
Console.WriteLine("Elements in HashSet1");
foreach(string val in set1){
Console.WriteLine(val);
}
HashSet<String> set2 = new HashSet<String>();
set2.Add("Tom");
set2.Add("Jack");
set2.Add("Kevin");
Console.WriteLine("Elements in HashSet2");
foreach(string val in set2){
Console.WriteLine(val);
}
Console.WriteLine("Do they share common elements? " + set1.Overlaps(set2));
}
}
The output of the above code is −
Elements in HashSet1 Nathan Tim Tom Jack Steve David Elements in HashSet2 Tom Jack Kevin Do they share common elements? True
Using Overlaps() with Arrays and Lists
The Overlaps() method works with any IEnumerable<T> collection, not just other HashSets −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<int> hashSet = new HashSet<int>{10, 20, 30, 40};
int[] array = {5, 15, 25, 30};
List<int> list = new List<int>{1, 2, 3, 4};
Console.WriteLine("HashSet overlaps with array: " + hashSet.Overlaps(array));
Console.WriteLine("HashSet overlaps with list: " + hashSet.Overlaps(list));
}
}
The output of the above code is −
HashSet overlaps with array: True HashSet overlaps with list: False
Conclusion
The Overlaps() method provides an efficient way to check if a HashSet shares any common elements with another collection. It works with any IEnumerable<T> collection and returns true if at least one element is found in both collections.
