如何在 Visual C# 中获取数组中的第二大数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1811846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the second highest number in an array in Visual C#?
提问by neuromancer
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?
我有一个整数数组。我想获得该数组中的第二大数字。是否有捷径可寻?
采纳答案by RCIX
Try this (using LINQ):
试试这个(使用 LINQ):
int secondHighest = (from number in numbers
orderby number descending
select number).Skip(1).First();
回答by Mitch Wheat
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). Now remove k elements from the heap; where each removal costs log(n) time to maintain the heap. Total time complexity = O(n + klogn)
要在未排序的数组中找到第 K 个最大元素:在 O(n) 中构建最大堆。现在从堆中删除 k 个元素;其中每次移除花费 log(n) 时间来维护堆。总时间复杂度 = O(n + klogn)
To understand building Max heap in O(n) see Binary heap
要了解在 O(n) 中构建最大堆,请参阅二进制堆
回答by Dani
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.
是的,有 2 个变量(第一次和第二次)通过数组,每次比较你得到的这两个单元格(总是把最高的放在第一个,第二个最高的放在第二个),一次通过你会得到第二个更高的第二个变种
回答by Gene Goykhman
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);
回答by Mukesh Kumar
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);
回答by Amresh Kumar
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());
回答by Eskinder Kassu
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];
}
}
回答by Jitendra Mahapatro
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();
}
回答by Arnold
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
首先获取最大数,一旦更改了最大数,就与第二个高数进行比较,看看它是否需要交换。第二个 if 语句检查该值是否小于最大值并大于第二个最大值。由于短路,如果第一个条件失败,则退出 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();
}