简体   繁体   English

如何在 Visual C# 中获取数组中第二大的数字?

[英]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 way to do this?是否有捷径可寻?

Try this (using LINQ): 试试这个(使用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. 您可以对数组进行排序并在第二个索引处选择项目,但是下面的O(n)循环会更快。

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);

是的,让2个变数(第一个和第二个)通过数组,每次通过这两个单元格来补偿您得到的结果(始终将最高的放在第一个,第二个最高的放在第二个)一次,您将在第二个上获得第二个变种

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)? 假设您的数组未排序,请参见: 如何在O(n)中长度为n的未排序数组中找到第k个最大元素?

To find Kth largest element in an unsorted array: Build a max heap in O(n). 要在未排序的数组中找到第K个最大元素:在O(n)中建立一个最大堆。 Now remove k elements from the heap; 现在从堆中删除k个元素; where each removal costs log(n) time to maintain the heap. 其中每次删除都花费log(n)时间来维护堆。 Total time complexity = O(n + klogn) 总时间复杂度= O(n + klogn)

To understand building Max heap in O(n) see Binary heap 要了解在O(n)中构建Max堆,请参阅二进制堆

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. 第二条if语句检查该值是否小于最大值且大于第二个最大值。 Because of the short circuit, if the first condition fails then it exits the if and skips 由于短路,如果第一个条件失败,则退出if并跳过

    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();
    }

There are two possibilities to find second highest number from an array.从数组中找到第二大数字有两种可能性。

1). 1). Find second max number from an array.从数组中找到第二个最大数。

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

int max = 0;
int second_max = 0;
foreach (int arr in myArray) {
    if (arr > max)
    {
        second_max = max;
        max = arr;
    }
}
Console.WriteLine("First highest number is: "+max);
Console.WriteLine("Second highest number is: " + second_max);

2). 2). Find second max number with the smallest complexity from an array.从数组中找到具有最小复杂度的第二个最大数。

int[] myArray = { 0, 2, 3, 13, 8}; //smaller number is given after larger number //小数在大数之后

int max = 0;
    int second_max = 0;
    foreach (int arr in myArray) {
        if (arr > max)
        {
            second_max = max;
            max = arr;
        }
        else if (arr > second_max)
        {
            second_max = arr;
        }
    }
    Console.WriteLine("First highest number is: "+max);
    Console.WriteLine("Second highest number is: " + second_max);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM