Second Highest Salary using C# Linq

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        int[] numbers = { 5, 12, 3, 9, 21, 21, 7 };

        var secondHighest = numbers
                            .Distinct() // Remove duplicates
                            .OrderByDescending(x => x) // Sort in descending order
                            .Skip(1) // Skip the highest
                            .FirstOrDefault(); // Take the second highest

        Console.WriteLine($"Second highest number is: {secondHighest}");
    }
}

Comments

Popular posts from this blog

API development using .NET Core - ProductManagmentCRUD

Singleton Design Pattern Realtime Example of banking

Consume API in MVC Application