找到复杂度最小的数组中的第二个最大数字

试图用Google搜索但没有运气。 如何在复杂度最小的数组中找到第二个最大数字?

代码或想法将有很大帮助。

我可以遍历一个数组并查找之后的最大数字,我有最大数字,然后再次循环数组,以相同的方式找到第二个。

但可以肯定的是效率不高。

您可以对数组进行排序并在第二个索引处选择项目,但是下面的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); 

要么

试试这个(使用LINQ):

 int secondHighest = (from number in test orderby number descending select number).Distinct().Skip(1).First() 

如何在Visual C#中获得数组中第二高的数字?

 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]; } 

你想要对数字进行排序,然后选择第二大数字。 这是一个不考虑效率的片段:

 var numbers = new int[] { 3, 5, 1, 5, 4 }; var result=numbers.OrderByDescending(x=>x).Distinct().Skip(1).First(); 

这不是太糟糕:

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

它的复杂性相当低,因为它最多只能排序三个元素

  static void Main(string[] args) { int[] myArray = new int[] { 0, 11, 2, 15, 16, 8, 16 ,8,15}; int Smallest = myArray.Min(); int Largest = myArray.Max(); foreach (int i in myArray) { if(i>Smallest && i 

即使您在数组中具有项目的声誉,这也将起作用

  int[] arr = {-10, -3, -3, -6}; int h = int.MinValue, m = int.MinValue; foreach (var t in arr) { if (t == h || t == m) continue; if (t > h) { m = h; h = t; } else if(t > m ) { m = t; } } Console.WriteLine("High: {0} 2nd High: {1}", h, m); //or, m = arr.OrderByDescending(i => i).Distinct().Skip(1).First(); Console.WriteLine("High: {0} 2nd High: {1}", h, m); 

对数组进行排序并获取倒数第二个值?

它不像你的结构是一棵树……它只是一个简单的数组,对吗?

最好的解决方案是对数组进行排序。 并且根据下降或上升,分别显示第二个或第二个最后一个元素。

另一种方法是使用一些内置方法,以获得初始最大值。 弹出该元素,然后再次搜索max。 不知道C#,所以不能给出直接的代码。

  var result = (from elements in inputElements orderby elements descending select elements).Distinct().Skip(1).Take(1); return result.FirstOrDefault(); 
 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(); } } } 

我的解决方案如下

  class Program { static void Main(string[] args) { Program pg = new Program(); Console.WriteLine("*****************************Program to Find 2nd Highest and 2nd lowest from set of values.**************************"); Console.WriteLine("Please enter the comma seperated numbers : "); string[] val = Console.ReadLine().Split(','); int[] inval = Array.ConvertAll(val, int.Parse); // Converts Array from one type to other in single line or Following line // val.Select(int.Parse) Array.Sort(inval); Console.WriteLine("2nd Highest is : {0} \n 2nd Lowest is : {1}", pg.Return2ndHighest(inval), pg.Return2ndLowest(inval)); Console.ReadLine(); } //Method to get the 2nd lowest and 2nd highest from list of integers ex 1000,20,-10,40,100,200,400 public int Return2ndHighest(int[] values) { if (values.Length >= 2) return values[values.Length - 2]; else return values[0]; } public int Return2ndLowest(int[] values) { if (values.Length > 2) return values[1]; else return values[0]; } } 

我在JavaScript中提供解决方案,找到最高和第二高的数字需要o(n / 2)复杂度。
这是工作的提琴手链接

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