Get the maximum value in the SortedSet in C#

The SortedSet<T> class in C# provides the Max property to efficiently retrieve the maximum value from the collection. Since SortedSet maintains elements in sorted order, the maximum element is always the last element in the collection.

Syntax

Following is the syntax for getting the maximum value from a SortedSet −

SortedSet<T> sortedSet = new SortedSet<T>();
T maxValue = sortedSet.Max;

Properties

Property Description Return Type
Max Gets the maximum value in the SortedSet according to the comparer. T
Min Gets the minimum value in the SortedSet according to the comparer. T

Using Max Property with String SortedSet

In this example, we create two SortedSet collections of strings and retrieve their maximum values −

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);
      
      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...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
      Console.WriteLine("Maximum element in SortedSet2 = " + set2.Max);
   }
}

The output of the above code is −

Elements in SortedSet1...
AB
BC
CD
EF
Maximum element in SortedSet1 = EF
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
Maximum element in SortedSet2 = JK

Using Max and Min Properties with Integer SortedSet

This example demonstrates how to get both maximum and minimum values from a numeric SortedSet −

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

How It Works

The SortedSet<T> collection uses a balanced binary search tree internally, which maintains elements in sorted order. The Max property provides O(log n) access to the maximum element, making it very efficient compared to iterating through the entire collection.

SortedSet Structure 50 20 70 10 30 95 ? Max Max property accesses rightmost node efficiently

Conclusion

The Max property of SortedSet<T> provides an efficient way to retrieve the maximum value from a sorted collection. Since SortedSet maintains elements in order, accessing the maximum element is optimized and doesn't require iteration through all elements.

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

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements