Get the minimum value in the SortedSet in C#

The SortedSet<T> class in C# provides built-in properties to efficiently retrieve the minimum and maximum values. The Min property returns the smallest element, while the Max property returns the largest element in the sorted collection.

Since SortedSet maintains elements in sorted order, accessing the minimum and maximum values is an O(1) operation, making it very efficient for scenarios where you frequently need these extreme values.

Syntax

Following is the syntax to get the minimum value from a SortedSet −

T minValue = sortedSet.Min;

Following is the syntax to get the maximum value from a SortedSet −

T maxValue = sortedSet.Max;

Using Min Property with String SortedSet

The following example demonstrates how to get minimum and maximum values from a SortedSet of strings −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("AB");
        set1.Add("BC");
        set1.Add("CD");
        set1.Add("EF");
        
        Console.WriteLine("Elements in SortedSet1...");
        foreach (string res in set1){
            Console.WriteLine(res);
        }
        
        Console.WriteLine("Maximum element in SortedSet1 = " + set1.Max);
        Console.WriteLine("Minimum element in SortedSet1 = " + set1.Min);
        
        SortedSet<string> set2 = new SortedSet<string>();
        set2.Add("BC");
        set2.Add("CD");
        set2.Add("DE");
        set2.Add("EF");
        set2.Add("AB");
        set2.Add("HI");
        set2.Add("JK");
        
        Console.WriteLine("Elements in SortedSet2 (Enumerator for SortedSet)...");
        SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext()) {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }
        
        Console.WriteLine("Maximum element in SortedSet2 = " + set2.Max);
        Console.WriteLine("Minimum element in SortedSet2 = " + set2.Min);
    }
}

The output of the above code is −

Elements in SortedSet1...
AB
BC
CD
EF
Maximum element in SortedSet1 = EF
Minimum element in SortedSet1 = AB
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK
Maximum element in SortedSet2 = JK
Minimum element in SortedSet2 = AB

Using Min Property with Integer SortedSet

The following example shows how to retrieve minimum and maximum values from a SortedSet of integers −

using System;
using System.Collections.Generic;

public class Demo {
    public static void Main(){
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(5);
        set1.Add(50);
        set1.Add(20);
        set1.Add(10);
        set1.Add(70);
        set1.Add(60);
        set1.Add(55);
        set1.Add(95);
        
        Console.WriteLine("Elements in SortedSet...");
        foreach (int res in set1){
            Console.WriteLine(res);
        }
        
        Console.WriteLine("Maximum element in SortedSet = " + set1.Max);
        Console.WriteLine("Minimum element in SortedSet = " + set1.Min);
    }
}

The output of the above code is −

Elements in SortedSet...
5
10
20
50
55
60
70
95
Maximum element in SortedSet = 95
Minimum element in SortedSet = 5

SortedSet Min/Max Access 5 10 20 50 55 60 70 95 Min Max Sorted Order (Automatic)

Key Points

  • The Min and Max properties provide O(1) time complexity access to extreme values.

  • Elements in SortedSet are automatically maintained in sorted order based on the default comparer or a custom comparer.

  • For string elements, sorting is done lexicographically (alphabetical order).

  • These properties will throw an exception if the SortedSet is empty.

Conclusion

The SortedSet's Min and Max properties provide efficient O(1) access to the smallest and largest elements. This makes SortedSet ideal for scenarios where you need both sorted storage and quick access to extreme values without having to iterate through the entire collection.

Updated on: 2026-03-17T07:04:36+05:30

182 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements