How to find highest ,second highest number, Lowest Second Lowest number in given Array
Solution 1
You can also try this -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class secondHighestLowest : System.Web.UI.Page
{
int[] arr = new int[10] { 45, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
protected void Page_Load(object sender, EventArgs e)
{
secondHighestLowestNumber();
secoundLowestNumber();
}
private void secondHighestLowestNumber()
{
int firstHighestNumber = arr[0];
int secondHighestNumber = arr[0];
for(int i = 0; i<arr.Length; i++)
{
if (arr[i]>firstHighestNumber)
{
firstHighestNumber = arr[i];
}
}
for (int x = 0; x < arr.Length; x++)
{
if (arr[x]>secondHighestNumber && firstHighestNumber!=arr[x])
{
secondHighestNumber = arr[x];
}
}
Response.Write("secondHighestNumber---- " + secondHighestNumber + "</br>");
}
private void secoundLowestNumber()
{
int firstLowestNumber = arr[0];
int secondLowestNumber = arr[0];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < firstLowestNumber)
{
firstLowestNumber = arr[i];
}
}
for (int x = 0; x < arr.Length; x++)
{
if (arr[x] < secondLowestNumber && firstLowestNumber != arr[x])
{
secondLowestNumber = arr[x];
}
}
Response.Write("secondLowestNumber---- " + secondLowestNumber + "</br>");
}
}
Hope this is helpful :)
Solution 2
Using Linq Concepts
var a = new int[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
var Max = a.Max(z => z);
var Min = a.Min( z => z);
var SMax = a.OrderByDescending(z=>z).Skip(1).First();
var SMin = a.OrderBy(z => z).Skip(1).First();
Solution 3
Assuming you have at least 2 items in the array you can use OrderBy()
and ElementAt()
:
var numbers = new[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
var secondLowest = numbers.OrderBy(num => num).ElementAt(1);
var secondHighest = numbers.OrderBy(num => num).Reverse().ElementAt(1);
Getting the highest and lowest is simpler and can be done using Max()
and Min()
LINQ methods.
var lowest = numbers.Min();
var highest = numbers.Max();
If you're worried about complexity, you can achieve better result using Selection algorithm. Using it you can perform the operations in O(n) complexity.
Solution 4
You don't specify the complexity requirement: one way is to sort the array in descending order and pick the top, second and third items.
Another is to build a Heap, and then perform remove root 3 times (with the heap being rebuilt after each remove).
Shaitender Singh
Updated on July 09, 2022Comments
-
Shaitender Singh almost 2 years
Can anyone Please tell me how to How to find highest ,second highest number, Lowest Second Lowest number in given Array
var numbers = new[] {855,3,64,6,24,75,3,6,24,45};
Any pointer and suggestion would really helpful . Thanks
-
Henk Holterman almost 14 yearsDidn't your teacher give you directions? (If it's homework, please use that tag)
-
-
Noon Silk almost 14 yearsThis, IMHO, is a shocking way to do this. Hence the downvote. (providing explanation, not argument).
-
Helen almost 14 yearsTo format a code block, indent it 4 spaces, or select it and click the code button (101010) on the editor's toolbar.
-
marc_s almost 14 years@silky: why is this so shocking?? Can you elaborate?? Using LINQ on a small collection like this sounds like a very natural and very obvious choice....
-
Chris over 13 yearsCreating a method that returns the highest value from an array of integers has actually been an interview question before. Max() actually turned out how to be the easiest solution.
-
Willy David Jr over 7 yearsSMax and SMin is only applicable if the array has no duplicate record. SMin still will return 3 as the answer which should be 6.
-
Aimal Khan almost 7 yearsdoes not work in case of int[] myArray = new int[] { 9, 4, 3, 6, 2 };