C# | Check if SortedSet and a specified collection share common elements

SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet.Overlaps(IEnumerable) Method is used to check whether a SortedSet and a specified collection share common elements or not.

Properties:

  • In C#, SortedSet class can be used to store, remove or view elements.
  • It maintains ascending order and does not store duplicate elements.
  • It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order.

Syntax:

mySortedSet1.Overlaps(mySortedSet2);

Here, mySortedSet1 and mySortedSet2 are two SortedSet.

Return Value: The function returns True if mySortedSet1 and mySortedSet2 share atleast one common element, else returns False.

Exception: This method will give ArgumentNullException if the SortedSet is null.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to Check if SortedSet
// and a specified collection
// share common elements
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet1 = new SortedSet<int>();
  
        // adding elements in mySortedSet1
        mySortedSet1.Add(5);
        mySortedSet1.Add(6);
        mySortedSet1.Add(7);
        mySortedSet1.Add(8);
        mySortedSet1.Add(9);
  
        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet2 = new SortedSet<int>();
  
        // adding elements in mySortedSet2
        mySortedSet2.Add(7);
        mySortedSet2.Add(8);
        mySortedSet2.Add(9);
        mySortedSet2.Add(10);
        mySortedSet2.Add(11);
  
        // Check if SortedSet and a specified
        // collection share common elements
        Console.WriteLine(mySortedSet1.Overlaps(mySortedSet2));
    }
}

chevron_right


Output:

True

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to Check if SortedSet
// and a specified collection
// share common elements
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet1 = new SortedSet<string>();
  
        // adding elements in mySortedSet1
        mySortedSet1.Add("A");
        mySortedSet1.Add("B");
        mySortedSet1.Add("C");
        mySortedSet1.Add("D");
        mySortedSet1.Add("E");
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet2 = new SortedSet<string>();
  
        // adding elements in mySortedSet2
        mySortedSet2.Add("F");
        mySortedSet2.Add("G");
        mySortedSet2.Add("H");
        mySortedSet2.Add("I");
        mySortedSet2.Add("J");
  
        // Check if SortedSet and a specified
        // collection share common elements
        Console.WriteLine(mySortedSet1.Overlaps(mySortedSet2));
    }
}

chevron_right


Output:

False

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.