C# 查找数组中复杂度最小的第二个最大数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14810444/
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
Find the second maximum number in an array with the smallest complexity
提问by E.Meir
Tried to googled it but with no luck. How can I find the second maximum number in an array with the smallest complexity?
试图用谷歌搜索它,但没有运气。如何在具有最小复杂度的数组中找到第二个最大数字?
code OR idea will be much help.
代码或想法会有很大帮助。
I can loop through an array and look for the maximum number after that, I have the maximum number and then loop the array again to find the second the same way.
我可以遍历一个数组并在此之后查找最大数,我有最大数,然后再次循环数组以相同的方式找到第二个。
But for sure it is not efficient.
但它肯定不是有效的。
采纳答案by andy
您可以对数组进行排序并选择第二个索引处的项目,但以下 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);
OR
或者
Try this (using LINQ):
试试这个(使用 LINQ):
int secondHighest = (from number in test
orderby number descending
select number).Distinct().Skip(1).First()
How to get the second highest number in an array in Visual C#?
回答by Captain Kenpachi
Sort the array and take the second to last value?
对数组进行排序并取倒数第二个值?
回答by Pratik Bothra
It's not like that your structure is a tree...It's just a simple array, right?
不是说你的结构是一棵树……它只是一个简单的数组,对吧?
The best solution is to sort the array. And depending on descending or ascending, display the second or the 2nd last element respectively.
最好的解决方案是对数组进行排序。并根据降序或升序分别显示第二个或倒数第二个元素。
The other alternative is to use some inbuilt methods, to get the initial max. Pop that element, and then search for the max again. Don't know C#, so can't give the direct code.
另一种选择是使用一些内置方法来获得初始最大值。弹出该元素,然后再次搜索最大值。不懂C#,所以不能直接给出代码。
回答by Dmitry Ledentsov
You'd want to sort the numbers, then just take the second largest. Here's a snippet without any consideration of efficiency:
你想对数字进行排序,然后只取第二大。这是一个没有考虑效率的片段:
var numbers = new int[] { 3, 5, 1, 5, 4 };
var result=numbers.OrderByDescending(x=>x).Distinct().Skip(1).First();
回答by Haithem KAROUI
public static int F(int[] array)
{
array = array.OrderByDescending(c => c).Distinct().ToArray();
switch (array.Count())
{
case 0:
return -1;
case 1:
return array[0];
}
return array[1];
}
回答by vikas_cool
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int size;
Console.WriteLine("Enter the size of array");
size = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the element of array");
int[] arr = new int[size];
for (int i = 0; i < size; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
int length = arr.Length;
Program program = new Program();
program.SeconadLargestValue(arr, length);
}
private void SeconadLargestValue(int[] arr, int length)
{
int maxValue = 0;
int secondMaxValue = 0;
for (int i = 0; i < length; i++)
{
if (arr[i] > maxValue)
{
secondMaxValue = maxValue;
maxValue = arr[i];
}
else if(arr[i] > secondMaxValue)
{
secondMaxValue = arr[i];
}
}
Console.WriteLine("First Largest number :"+maxValue);
Console.WriteLine("Second Largest number :"+secondMaxValue);
Console.ReadLine();
}
}
}
回答by Gaurav Bhushan
var result = (from elements in inputElements
orderby elements descending
select elements).Distinct().Skip(1).Take(1);
return result.FirstOrDefault();
回答by Praveen M P
I am giving solution that's in JavaScript, it takes o(n/2) complexity to find the highest and second highest number.
here is the working Fiddler Link
我给出了 JavaScript 中的解决方案,找到最高和第二高的数字需要 o(n/2) 复杂度。
这是工作中的Fiddler 链接
var num=[1020215,2000,35,2,54546,456,2,2345,24,545,132,5469,25653,0,2315648978523];
var j=num.length-1;
var firstHighest=0,seoncdHighest=0;
num[0] >num[num.length-1]?(firstHighest=num[0],seoncdHighest=num[num.length-1]):(firstHighest=num[num.length-1], seoncdHighest=num[0]);
j--;
for(var i=1;i<=num.length/2;i++,j--)
{
if(num[i] < num[j] )
{
if(firstHighest < num[j]){
seoncdHighest=firstHighest;
firstHighest= num[j];
}
else if(seoncdHighest < num[j] ) {
seoncdHighest= num[j];
}
}
else {
if(firstHighest < num[i])
{
seoncdHighest=firstHighest;
firstHighest= num[i];
}
else if(seoncdHighest < num[i] ) {
seoncdHighest= num[i];
}
}
}
回答by Enigmativity
This isn't too bad:
这还不错:
int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
var secondMax =
myArray.Skip(2).Aggregate(
myArray.Take(2).OrderByDescending(x => x).AsEnumerable(),
(a, x) => a.Concat(new [] { x }).OrderByDescending(y => y).Take(2))
.Skip(1)
.First();
It's fairly low on complexity as it only every sorts a maximum of three elements
它的复杂性相当低,因为它最多只能对三个元素进行排序
回答by ajay radam
namespace FindSecondLargestNumber
{
class Program
{
static void Main(string[] args)
{
int max=0;
int smax=0;
int i;
int[] a = new int[20];
Console.WriteLine("enter the size of the array");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("elements");
for (i = 0; i < n; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
for (i = 0; i < n; i++)
{
if ( a[i]>max)
{
smax = max;
max= a[i];
}
else if(a[i]>smax)
{
smax=a[i];
}
}
Console.WriteLine("max:" + max);
Console.WriteLine("second max:"+smax);
Console.ReadLine();
}
}
}