开发者

How to get the second highest number in an array in Visual C#?

I have an array of ints. I want to get the second highest number in that array. Is there an easy 开发者_StackOverflowway to do this?


Try this (using LINQ):

int secondHighest = (from number in numbers
                     orderby number descending
                     select number).Skip(1).First();


You could sort the array and choose the item at the second index, but the following O(n) loop will be much faster.

int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
int largest = int.MinValue;
int second = int.MinValue;
foreach (int i in myArray)
{
    if (i > largest)
    {
        second = largest;
        largest = i;
    }
    else if (i > second)
        second = i;
}

System.Console.WriteLine(second);


Yes, have 2 vars (first and second) passthrough the array and each time compair what you get with this two cells (always putting the highest on first and the 2nd highest on second) with one pass you will get the 2nd higher on the second var.


You don't specify if you want to do this with the minimum complexity.

Assuming your array is unsorted, please see: How to find the kth largest element in an unsorted array of length n in O(n)?

To find Kth largest element in an unsorted array: Build a max heap in O(n). Now remove k elements from the heap; where each removal costs log(n) time to maintain the heap. Total time complexity = O(n + klogn)

To understand building Max heap in O(n) see Binary heap


max1=0;
max2=0;

for( int i=0; i < a.Length; i++)
{
    if (arr[i]> max1)
    {
        max2=max1;
        max1=arr[i];
    }
    else
    {
       if (a[i]!= max1) && ( a[i] > max2)
          max2[i]=arr[i];
    }
}


Getting the max number first, once the max is changed do a comparison against the second high number to see if it needs to swapped. The second if statement checks if the value is less than the max and is greater than the second highest value. Because of the short circuit, if the first condition fails then it exits the if and skips

    static void Main(string[] args)
    {
        //int[] arr = new int[10] { 9, 4, 6, 2, 11, 100, 53, 23, 72, 81 };
        int[] arr = { 1, 8, 4, 5, 12, 2, 5, 6, 7, 1, 90, 100, 56, 8, 34 };
        int MaxNum = 0;
        int SecNum = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] > MaxNum)
            {
                if (MaxNum > SecNum) { SecNum = MaxNum; }
                MaxNum = arr[i];
            }

            if (arr[i] < MaxNum && arr[i] > SecNum)
            {
                SecNum = arr[i];
            }
        }

        Console.WriteLine("Highest Num: {0}. Second Highest Num {1}.", MaxNum, SecNum);
        Console.ReadLine();
    }


    int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
    int num1=0, temp=0;
    for (int i = 0; i < myArray.Length; i++)
    {
        if (myArray[i] >= num1)
        {
            num1 = myArray[i];
        }
        else if ((myArray[i] < num1) && (myArray[i] > temp))
        {
            temp = myArray[i];
        }
    }
    Console.WriteLine("The Largest Number is: " + num1);
    Console.WriteLine("The Second Highest Number is: " + temp);


int[] arr = { 1, 8, 4, 5, 12, 2, 5, 6, 7, 1, 90, 100, 56, 8, 34 };

int first, second;
// Assuming the array has at least one element:
first = second = arr[0];
for(int i = 1; i < arr.Length; ++i)
{
  if (first < arr[i])
  {
    // 'first' now contains the 2nd largest number encountered thus far:
    second = first;
    first = arr[i];
  }

}
MessageBox.Show(second.ToString());


    static void Main(string[] args)
    {
        int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5,12,11,14 };
        int num1 = 0, temp = 0;
        for (int i = 0; i < myArray.Length; i++)
        {
            if (myArray[i] >= num1)
            {
                temp = num1;
                num1 = myArray[i];
            }
            else if ((myArray[i] < num1) && (myArray[i] > temp))
            {
                temp = myArray[i];
            }
        }
        Console.WriteLine("The Largest Number is: " + num1);
        Console.WriteLine("The Second Highest Number is: " + temp);
        Console.ReadKey();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜