Author -  Sai gowtham

How to get the second highest number from a array in C#

Learn, how to get the second highest number from a array in C# with the help of examples.

Consider, we have a following array of numbers in our code.

int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };

Now, we need to get the second highest 8 number from a above array in C#.

To get the second highest number from a array, first we need to sort the array using the Array.sort() method then get the second highest number using the following index [myArray.Length-2].

Here is an example:

using System;
using System.Linq;
using System.Collections.Generic;

class GetSecondHighestNumber {
  static void Main() {
    int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
    Array.Sort(myArray);
    int secondHighest = myArray[myArray.Length-2];
    Console.WriteLine(secondHighest);
  }
}

Output:

8

In the example above, we first sorted the array using the Array.Sort() method by passing the array.

The Array.sort() returns the array in ascending order, so, we can get the second highest number by using the myArray[myArray.Length-2].

Similarly, we can also get the second highest number in C# by using the following Linq syntax.

using System;
using System.Linq;
using System.Collections.Generic;

class GetSecondHighestNumber {
  static void Main() {
     int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
    int secondHighest = (from number in myArray
                     orderby number descending
                     select number).Skip(1).First();
    Console.WriteLine(secondHighest);
  }
}

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY