C# SortedSet
SortedSet in C# is a collection of objects that stores elements uniquely in sorted order. It is of the generic type collection and is defined under System.Collections.Generic namespace. It is a dynamic collection means the size of the SortedSet is automatically increased when new elements are added. There are some key features mentioned below:
- Unique and Sorted: SortedSet is used to store elements uniquely in sorted order.
- Mathematical Operations: provides many mathematical set operations, such as intersection, union, and difference.
- Optimal: Perform insert, delete, and search operations with O(log n) time complexity.
Example: Creating and Displaying a SortedSet
// C# program to demonstrate the use of SortedSet
using System;
using System.Collections.Generic;
class Geeks {
public static void Main() {
// Creating a SortedSet
SortedSet<int> num =
new SortedSet<int> { 7, 1, 2, 8, 1, 4 };
// Adding elements
num.Add(6);
// Adding duplicate (will not be added)
num.Add(2);
// Displaying elements
Console.WriteLine("SortedSet elements:");
foreach (int ele in num)
Console.Write(ele + " ");
}
}
Output
SortedSet elements: 1 2 4 6 7 8
Steps to Create SortedSet
SortedSet Class provides 7 different types of constructors for creating a SortedSet, here we use SortedSet(), constructor, which is used to create an instance of the SortedSet class.
Step 1: Include System.Collections.Generic namespace
using System.Collections.Generic;
Step 2: Create a SortedSet using the SortedSet Class
SortedSet<type_of_sortedset> sortedset_name = new SortedSet<type_of_sortedset>();
Performing Different Operations on SortedSet
1. Adding Element
- Add(): We can use the Add method to put elements in the sorted set
- Collection-Initializer: Using the collection initializer we can directly put the element with initialization.
// Using the Add() method
SortedSet<int> num= new SortedSet<int>();
num.Add(1);
numAdd(2);// Using Collection Initalizer
SortedSet<int> num = new SortedSet<int> { 2, 1, 4,3 };
2. Accessing Elements
Using foreach loop: We can use for loop to Iterate each element to access the the sorted set
SortedSet<int> num = new SortedSet<int> { 2,3,4,5};
foreach (int ele in num)
{
Console.WriteLine(ele);
}
Using ForEach loop with Lambda Expressions: We can use the ForEach loop with lambda expressions to access the elements of sorted set which reduces the number of code lines and enhances the code readability
SortedSet<int> num = new SortedSet<int> { 2,3,4,5};
numbers.ToList().ForEach(num => Console.WriteLine(num));
Using ElementAt() with LINQ: The SortedSet does not directly support the Indexes but we can use LINQ and use ElementAt() method.
SortedSet<int> num = new SortedSet<int> { 2,3,4,5 };
Console.WriteLine(num.ElementAt(0));
Example: Accessing Elements Using Different Methods
// C# program to demonstrate various
// ways to access elements in a SortedSet
using System;
using System.Linq;
using System.Collections.Generic;
class Geeks {
static void Main() {
// Creating a SortedSet using Collection Initializer
SortedSet<int> num = new SortedSet<int> { 2, 3, 4, 5 };
// Accessing elements using foreach loop
Console.WriteLine("Accessing using foreach loop:");
foreach (int ele in num)
Console.Write(ele + " ");
Console.WriteLine();
// Creating a SortedSet using Add() method
SortedSet<int> num2 = new SortedSet<int>();
num2.Add(1);
num2.Add(2);
num2.Add(3);
// Accessing elements using ForEach loop
Console.WriteLine("Accessing using ForEach loop:");
num2.ToList().ForEach(ele => Console.Write(ele + " "));
Console.WriteLine();
}
}
Output
Accessing using foreach loop: 2 3 4 5 Accessing using ForEach loop: 1 2 3
3. Removing Elements
In SortedSet, we are allowed to remove elements from the SortedSet. The sorted set <T> class provides three different methods to remove elements and the methods are:
- Remove(T): This method is used to remove a specified item from the SortedSet.
- RemoveWhere(Predicate): This method is used to remove all elements that match the conditions defined by the specified predicate from a SortedSet.
- Clear(): This method is used to remove all elements from the set.
Example:
// Removing elements from SortedSet
using System;
using System.Collections.Generic;
class Geeks
{
static public void Main()
{
// Creating SortedSet
// Using SortedSet class
SortedSet<int> set = new SortedSet<int>();
// Add the elements in SortedSet
// Using Add method
set.Add(1);
set.Add(2);
set.Add(3);
set.Add(4);
// After using Remove method
Console.WriteLine("Total number of elements " +
"present in set:{0}", set.Count);
// Remove element from SortedSet
// Using Remove method
set.Remove(1);
// Before using Remove method
Console.WriteLine("Total number of elements " +
"present in set:{0}", set.Count);
// Remove all elements from SortedSet
// Using Clear method
set.Clear();
Console.WriteLine("Total number of elements " +
"present in set:{0}", set.Count);
}
}
Output
Total number of elements present in set:4 Total number of elements present in set:3 Total number of elements present in set:0
4. Check If an Element Exists
In SortedSet, we can check the presence of an element using the Contains method. This method is used to determine whether the set contains a specific element.
Example:
// C# program to illustrate how to check
// availability of elements in SortedSet
using System;
using System.Collections.Generic;
public class Geeks{
static public void Main()
{
// Creating SortedSet
// Using SortedSet class
SortedSet<int> set = new SortedSet<int>();
// Add the elements in SortedSet
// Using Add method
set.Add(1);
set.Add(2);
set.Add(3);
set.Add(4);
// Check the availability of element
// Using Contains method
if (set.Contains(1))
Console.WriteLine("Element is available.");
else
Console.WriteLine("Element is not available.");
}
}
Output
Element is available.
Important Points
- The SortedSet class implements the
- ICollection, IEnumerable, IReadOnlyCollection, ISet, ICollection, IEnumerable,
- IDeserializationCallback, and ISerializable interfaces.
- The capacity of a SortedSet is the number of elements it can hold.
- In SortedSet, the elements must be unique.
- In SortedSet, the order of the element is ascending.
- It is generally used when we want to use SortedSet class if we have to store unique elements and maintain ascending order.
- In SortedSet, the user can only store the same type of elements.