SortedSet<T> is a collection of unique elements that are automatically sorted in ascending order by default. It is implemented using a balanced binary search tree (red-black tree), which provides efficient insertion, deletion and search operations.
There are some key features mentioned below:
- Unique and Sorted: Automatically stores elements without duplicates in sorted order.
- Set Operations: Supports mathematical operations such as UnionWith, IntersectWith and ExceptWith.
- Optimal: Add, Remove and Contains operations have O(log n) time complexity.
Creating a SortedSet
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>();
Example:
C# using System; using System.Collections.Generic; class Geeks { static void Main() { // Create a SortedSet using collection initializer SortedSet<int> numbers = new SortedSet<int> { 7, 1, 2, 8, 1, 4 }; // Add elements numbers.Add(6); // Attempt to add duplicate (will not be added) numbers.Add(2); Console.WriteLine("SortedSet elements:"); foreach (int num in numbers) Console.Write(num + " "); } } OutputSortedSet elements: 1 2 4 6 7 8
Note: Duplicate elements are automatically ignored.
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);
num.Add(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> numbers= new SortedSet<int> { 2,3,4,5};
foreach (int num in numbers)
Console.WriteLine(num);
Using ForEach loop with Lambda Expressions: Lambda expressions reduces the number of code lines and enhances the code readability.
SortedSet<int> numbers = new SortedSet<int> { 2, 3, 4, 5 };
numbers.ToList().ForEach(n => Console.WriteLine(n));
Using ElementAt() with LINQ: The SortedSet does not directly support the Indexes but we can use LINQ and use ElementAt() method.
using System.Linq;
SortedSet<int> numbers= new SortedSet<int> { 2,3,4,5 };
Console.WriteLine(numbers.ElementAt(0));
Example: Accessing Elements Using Different Methods
C# 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(); } } OutputAccessing using foreach loop: 2 3 4 5 Accessing using ForEach loop: 1 2 3
3. Removing Elements
SortedSet<T> provides three ways to remove elements:
Example:
C# using System; using System.Collections.Generic; class Geeks { static public void Main() { // Creating a SortedSet SortedSet<int> set = new SortedSet<int>(); // Adding elements to the SortedSet set.Add(1); set.Add(2); set.Add(3); set.Add(4); // Display total elements before removal Console.WriteLine("Total number of elements present in set: {0}", set.Count); // Remove a specific element set.Remove(1); // Display total elements after removing one element Console.WriteLine("Total number of elements present in set after Remove(1): {0}", set.Count); // Remove all elements using Clear method set.Clear(); // Display total elements after clearing the set Console.WriteLine("Total number of elements present in set after Clear(): {0}", set.Count); } } OutputTotal number of elements present in set: 4 Total number of elements present in set after Remove(1): 3 Total number of elements present in set after Clear(): 0
4. Check If an Element Exists
We can check the presence of an specific element using the Contains method.
Example:
C# 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 if the element exists in the SortedSet if (set.Contains(1)) Console.WriteLine("Element is available."); else Console.WriteLine("Element is not available."); } } OutputElement is available.
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers