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 SortedSet and a specified collection share common elements in C#
To check if a SortedSet and a specified collection share common elements in C#, you can use the Overlaps() method. This method returns true if the SortedSet shares at least one element with the specified collection, otherwise it returns false.
Syntax
Following is the syntax for using the Overlaps() method −
public bool Overlaps(IEnumerable<T> other)
Parameters
other − The collection to compare with the current
SortedSet. It can be any collection that implementsIEnumerable<T>.
Return Value
The method returns true if the SortedSet shares at least one element with the specified collection; otherwise, it returns false.
Example - Sets with Common Elements
The following example demonstrates checking for overlaps between two SortedSet collections that share common elements −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
set1.Add(400);
set1.Add(500);
set1.Add(600);
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(100);
set2.Add(200);
set2.Add(300);
set2.Add(400);
set2.Add(500);
set2.Add(600);
Console.WriteLine("Does it share common elements? = " + set1.Overlaps(set2));
}
}
The output of the above code is −
Does it share common elements? = True
Example - Sets with No Common Elements
The following example shows checking for overlaps between two SortedSet collections that have no common elements −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(450);
set2.Add(550);
set2.Add(650);
set2.Add(750);
set2.Add(800);
Console.WriteLine("Does it share common elements? = " + set1.Overlaps(set2));
}
}
The output of the above code is −
Does it share common elements? = False
Using Overlaps with Different Collection Types
The Overlaps() method works with any collection that implements IEnumerable<T>, not just other SortedSet instances −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedSet<string> fruits = new SortedSet<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Orange");
List<string> checkList = new List<string>{"Banana", "Mango", "Grapes"};
Console.WriteLine("SortedSet overlaps with List: " + fruits.Overlaps(checkList));
string[] checkArray = {"Cherry", "Pineapple", "Kiwi"};
Console.WriteLine("SortedSet overlaps with Array: " + fruits.Overlaps(checkArray));
}
}
The output of the above code is −
SortedSet overlaps with List: True SortedSet overlaps with Array: False
Conclusion
The Overlaps() method in C# provides an efficient way to check if a SortedSet shares any common elements with another collection. This method returns true if at least one element is found in both collections, making it useful for set operations and data validation scenarios.
