Get The Minimum Value In The Sortedset In C#

The SortedSet represents a sorted collection in C# programming, ensuring that elements are arranged in a specific order. Similar to List and HashSet, this collection manages the sorted sequence, removing the requirement for manual sorting tasks. Accessing the Min property of the SortedSet allows you to easily retrieve the smallest value within the collection, streamlining the process without the necessity of iterating through all elements to locate the minimum value.

When analyzing the time complexity of fetching the smallest value from a SortedSet, the Min property offers a constant time complexity of O(1) for retrieving the minimum element. Therefore, it is more efficient to utilize the Min property instead of iterating through the complete collection.

Syntax:

It has the following syntax:

Example

int minValue = sortedSet.Min;

This method does not accept any arguments and retrieves the smallest element from the specified SortedSet in C#.

Uses of Min property

The primary benefit of this characteristic is its ability to provide the lowest value within a consistent time complexity.

Example:

Let's consider a program in C# to retrieve the lowest value from a sorted collection.

Example

using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
    static void Main(string[] args)
    {
        // Create a SortedSet of strings
        SortedSet<string> sortedSet = new SortedSet<string>();
        // Adding three elements to the SortedSet
        sortedSet.Add("banana");
        sortedSet.Add("apple");
        sortedSet.Add("orange");
        // Retrieve the first element using the First() method
        string firstElement = sortedSet.First();
        // finding the first letter from the first element.
        char firstLetter = firstElement[0];
        // Display the first letter
        Console.WriteLine("First letter in SortedSet: " + firstLetter); // Output: a
    }
}

Output:

The given code snippet defines a CSS class for a placeholder diagram. This class sets the background color using a linear gradient, adds border radius, padding, margin, and centers the content. Additionally, it specifies styles for the placeholder icon and text within the diagram.

Explanation:

In the provided C# code snippet, a sorted set is populated with various strings, including "banana", "apple", and "orange". By leveraging the SortedSet data structure, the strings are automatically arranged in ascending order. Subsequently, to access the smallest string within the collection, the First method is employed. This particular method is designed to retrieve the string that holds the lowest lexicographical value. Through standard indexing, the initial character of the smallest string is obtained and displayed. The SortedSet collection along with the First method is instrumental in identifying the minimum element in this program.

Example 2:

Let's consider another sample program to demonstrate the usage of SortedSet in C#.

Example

using System;
using System.Collections.Generic;
class Program
{
    static void Main(string[] args)
    {
        // Create a SortedSet of integers
        SortedSet<int> sortedSet = new SortedSet<int>();
        Console.WriteLine("Enter integers to add to the SortedSet. Enter 'done' to finish.");
        while (true)
        {
            Console.Write("Enter an integer: ");
            string input = Console.ReadLine();
            if (input.ToLower() == "done")
                break;
            if (int.TryParse(input, out int number))
            {
                sortedSet.Add(number);
            }
            else
            {
                Console.WriteLine("Invalid input. Please enter a valid integer.");
            }
        }
        if (sortedSet.Count == 0)
        {
            Console.WriteLine("No integers added.");
            return;
        }
        // Retrieve the minimum value from the SortedSet
        int minValue = sortedSet.Min;
        Console.WriteLine("Minimum value in SortedSet: " + minValue);
    }
}

Output:

The <style> code snippet demonstrates a placeholder diagram with specific styling. The background is a linear gradient with a border radius of 12px, padding of 40px, and a margin of 20px on the top and bottom. The content is centered within the diagram. Additionally, the placeholder icon has a font size of 3rem and a margin bottom of 10px, while the placeholder text is styled with a color of #9ca3af and a font size of 1rem. This design creates a visually appealing placeholder for various web elements.

Explanation:

In this software, users are required to enter numerical values, which are subsequently inserted into the SortedSet data structure. To view the outcome, users must enter "done" to terminate the input stream and display the result. Each input is parsed into an integer using the int.TryParse function before being added to the SortedSet collection. The smallest value is determined using the Min property of the SortedSet and then displayed on the console.

Small Application demonstrating the importance of the SortedSet in C#.

Example

using System;
using System.Collections.Generic;
class Program
{
    static void Main(string[] args)
    {
        // Initialize a SortedSet to store chores based on priority
        SortedSet<Chore> chores = new SortedSet<Chore>();
        Console.WriteLine("Enter chores and their priorities (Low, Medium, or High). Enter 'done' to finish.");
        while (true)
        {
            Console.Write("Chore: ");
            string choreName = Console.ReadLine();
            if (choreName.ToLower() == "done")
                break;
            Console.Write("Priority (Low, Medium, High): ");
            string priorityStr = Console.ReadLine();
            Priority priority;
            if (!Enum.TryParse(priorityStr, true, out priority))
            {
                Console.WriteLine("Invalid priority. Please enter Low, Medium, or High.");
                continue;
            }
            chores.Add(new Chore(choreName, priority));
        }
        if (chores.Count == 0)
        {
            Console.WriteLine("No chores added.");
            return;
        }
        // Retrieve and perform the chore with the Lowest priority (minimum value)
        Chore highestPriorityChore = chores.Min;
        Console.WriteLine($"Lowest priority chore: {highestPriorityChore.Name}");
        chores.Remove(highestPriorityChore);
        Console.WriteLine("Remaining chores:");
        foreach (var chore in chores)
        {
            Console.WriteLine(chore.Name);
        }
    }
}
enum Priority
{
    Low,
    Medium,
    High
}
class Chore : IComparable<Chore>
{
    public string Name { get; set; }
    public Priority Priority { get; set; }

    public Chore(string name, Priority priority)
    {
        Name = name;
        Priority = priority;
    }
    public int CompareTo(Chore other)
    {
        return Priority.CompareTo(other.Priority);
    }
}

Output:

The CSS code snippet below demonstrates the styling for a placeholder diagram:

Example

.placeholder-diagram { background: linear-gradient(135deg, #374151 0%, #1f2937 100%); border-radius: 12px; padding: 40px; margin: 20px 0; text-align: center; }
.placeholder-diagram .placeholder-icon { font-size: 3rem; margin-bottom: 10px; }
.placeholder-diagram .placeholder-text { color: #9ca3af; font-size: 1rem; }

Explanation:

This software enables users to input tasks along with their importance levels categorized as low, medium, or high. The primary objective of the application is to identify and eliminate tasks marked with low priority from the collection. Initially, it employs a while loop to consistently receive user inputs. The loop can be concluded by entering "done" once all tasks have been added to the SortedSet data structure. By utilizing the Min property, it extracts the lowest priority task from the set. Subsequently, all the removed elements are showcased. This application effectively organizes and prioritizes tasks according to the specified category.

Input Required

This code uses input(). Please provide values below: