Reputation: 2207
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
Upvotes: 1
Views: 32037
Reputation: 27
Shortest way to find out largest, second largest, smallest & second smallest number, using conventional method:
int LargestNumber = 0;
int SecondLargestNumber = 0;
int SmallestNumber = 0;
int SecondSmallestNumber = 0;
int[] array = new int[] { 250, 5, 17, 50, 98, 352, 2, 8, 67, 150, 200, 1 };
for (int i = 0; i < array.Length; i++)
{
if (array[i] > LargestNumber)
{
SecondLargestNumber = LargestNumber;
LargestNumber = array[i];
}
else if (array[i] > SecondLargestNumber)
{
SecondLargestNumber = array[i];
}
if (i == 0)
{
SmallestNumber = array[0];
SecondSmallestNumber = array[1];
}
if (array[i] < SmallestNumber)
{
SecondSmallestNumber = SmallestNumber;
SmallestNumber = array[i];
}
else if (array[i] < SecondSmallestNumber)
{
SecondSmallestNumber = array[i];
}
}
Console.WriteLine("Largest Number : " + LargestNumber);
Console.WriteLine("Second Largest Number : " + SecondLargestNumber);
Console.WriteLine("Smallest Number : " + SmallestNumber);
Console.WriteLine("Second Smallest Number : " + SecondSmallestNumber);
Upvotes: 0
Reputation: 585
Array.Sort(new);
string newNumber = string.Join(" ", new[0]);
return newNumber;
Array.Sort(new);
string newNumber = string.Join(" ", new[1]);
return newNumber;
Array.Sort(new);
string newNumber = string.Join(" ", new[new.Length - 1]);
return newNumber;
Array.Sort(new);
string newNumber = string.Join(" ", new[new.Length - 1]);
return newNumber;
Upvotes: 0
Reputation: 9171
This algorithm is applicable even with duplicate record.
int[] intArrayInput= new int[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45, 855 };
Using Sort:
Array.Sort(intArrayInput);
int intMax = intArrayInput[intInput.Length - 1];
int intLow = intArrayInput[0];
And using LINQ:
int intSecondMax = intArrayInput.OrderByDescending(x => x).Distinct().Skip(1).First();
int intSecondLow = intArrayInput.OrderBy(x => x).Distinct().Skip(1).First();
Upvotes: 0
Reputation: 101
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();
Upvotes: 4
Reputation: 83
I have first arranged them using Selection Sort algorithm in ascending order, then i displayed values.
static void Main(string[] args)
{
int[] Num =new int[] { 3, 4, 5, 6, 7, 0,99,105,55 };
int temp;
for(int a=0;a<Num.Length-1;a++)
{
for(int b=a+1;b<Num.Length;b++)
{
if(Num[a]>Num[b])
{
temp = Num[a];
Num[a] = Num[b];
Num[b] = temp;
}
}
}
Console.WriteLine("Max value ="+Num[Num.Length-1]+"\nSecond largest Max value="+ Num[Num.Length - 2]+"\nMin value =" + Num[0] + "\nSecond smallest Min value=" + Num[1]);
Console.WriteLine("\nPress to close");
Console.ReadLine();
}
Upvotes: 0
Reputation: 43
Why two loops when can be done in
int[] myArray = new int[] { 2, 4, 3, 6, 9 };
int max1 = 0;
int max2 = 0;
for (int i = 0; i < myArray.Length; i++)
{
if (myArray[i] > max1)
{
max2 = max1;
max1 = myArray[i];
}
else
{
max2 = myArray[i];
}
}
Console.WriteLine("first" + max1.ToString());
Console.WriteLine("Second" + max2.ToString());
Console.ReadKey();
Upvotes: 0
Reputation: 1
int[] myUnSortArray = { 1, 5, 8, 3, 10, 6, 19, 5, 4, 4 };
int[] SortedArray = (from number in myUnSortArray
orderby number ascending
select number).ToArray();
int highestValue = SortedArray.Max();
int SecondHighest = SortedArray.Last(m => m < highestValue);
Upvotes: -1
Reputation: 1
int[] i = new int[] { 4, 8, 1, 9, 2, 7, 3 };
Array.Sort(i);
Console.WriteLine("Highest number :" + i[i.Length-1]);
Console.WriteLine("Second highest number :"+i[i.Length-2]);
Console.WriteLine("Lowest number :" + i[i.Length-i.Length]);
Console.WriteLine("Second Lowest number :" + i[i.Length -i.Length+1]);
Output : Highest number : 9
Second highest number : 8
Lowest number : 1
Second Lowest number : 2
Upvotes: 0
Reputation: 1975
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 :)
Upvotes: 4
Reputation: 23800
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.
Upvotes: 4
Reputation: 300827
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).
Upvotes: 4