Get the maximum value in the SortedSet in C#

CsharpServer Side ProgrammingProgramming

To get the maximum value in the SortedSet, the code is as follows −

Example

 Live Demo

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 (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);
   }
}

Output

This will produce the following output −

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

Example

Let us see another example −

 Live Demo

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);
   }
}

Output

This will produce the following output −

Elements in SortedSet...
5
10
20
50
55
60
70
95
Maximum element in SortedSet = 95 Minimum element in SortedSet = 5
raja
Published on 06-Dec-2019 10:35:26
Advertisements