Get the minimum value in the SortedSet in C#The SortedSet is a collection in C# programming that maintains a sorted order of the given elements. Other collections like List and HashSet also maintain the sorted order, eliminating the need for manual sorting operations. The Min property of the SortedSet is used to find the minimum value in the collection. It provides an efficient access to the smallest element. It will eliminate the extra iteration of entire elements to find the minimum element. When we consider the time complexity of retrieving the minimum value from a SortedSet, the Min property provides constant time complexity, that is O(1) for accessing the smallest element, so it is better to use the Min property than iterating through the entire collection. Syntax:It has the following syntax: This property does not take any parameters. It returns the minimum element from the given SortedSet in C# Uses of Min propertyThe main advantage of this property is that it will return the minimum value within fixed time complexity Example:Let us take a program to get the minimum value in the sorted set in C#. Output: Explanation: In the above C# program, a sorted set contains some strings. Here, only three strings are "banana", "apple", and "orange". As we used the SortedSet, the elements are sorted. After that, the minimum string is present at the beginning of the collection, so we use the First() method to get the minimum string in the set. This method will return the smallest lexicographical string. We used normal indexing to retrieve the first character in the smallest string and then printed the smallest character in the smallest string. We used this program's SortedSet collection and the First method to get the minimum element. Example 2:Let us take another example program to illustrate the SortedSet in C#. Output: Explanation: In this program, the user has to input the numbers. After that, they are added to the SortedSet data structure. If the user has to see the result, user has to give input as "done". It makes the input stream end and gives the result. Each element is converted to an integer by using the int.TryParse() method, and then they are stored in the SortedSet. The minimum is found using the Min property from the SortedSet and printed to the console. Small Application demonstrating the importance of the SortedSet in C#.Output: Explanation: This program allows users to input chores and their priorities. The priority may be low, medium, or high. The main task of the program is to find the tasks with low priority and remove them from the List. Initially, it uses a while loop to continuously take input from the user. This loop can be terminated by giving input as "done" after adding all the chores to the SortedSet. It uses the Min property and removes it from the set. After removing all the elements, they are displayed. This program will efficiently manage and prioritize chores based on the given tag. |