简体   繁体   English

如何在给定数组中找到最高,第二高的数字,最低的第二低的数字

[英]How to find highest ,second highest number, Lowest Second Lowest number in given Array

Can anyone Please tell me how to How to find highest ,second highest number, Lowest Second Lowest number in given Array任何人都可以请告诉我如何在给定数组中找到最高,第二高的数字,最低的第二低的数字

var numbers = new[] {855,3,64,6,24,75,3,6,24,45};

Any pointer and suggestion would really helpful .任何指针和建议都会非常有帮助。 Thanks谢谢

Using Linq Concepts使用 Linq 概念

var a = new int[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45 };

var Max = a.Max(z => z);
var Min = a.Min( z => z);
var SMax = a.OrderByDescending(z=>z).Skip(1).First();
var SMin = a.OrderBy(z => z).Skip(1).First();

You don't specify the complexity requirement: one way is to sort the array in descending order and pick the top, second and third items.您没有指定复杂性要求:一种方法是按降序对数组进行排序,然后选择顶部、第二个和第三个项目。

Another is to build a Heap, and then perform remove root 3 times (with the heap being rebuilt after each remove).另一种是构建一个Heap,然后执行3次remove root(每次移除后重建堆)。

Assuming you have at least 2 items in the array you can use OrderBy() and ElementAt() :假设您在数组中至少有 2 个项目,您可以使用OrderBy()ElementAt()

var numbers = new[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45 };
var secondLowest = numbers.OrderBy(num => num).ElementAt(1);
var secondHighest = numbers.OrderBy(num => num).Reverse().ElementAt(1);

Getting the highest and lowest is simpler and can be done using Max() and Min() LINQ methods.获取最高和最低值更简单,可以使用Max()Min() LINQ 方法完成。

var lowest = numbers.Min();
var highest = numbers.Max();

If you're worried about complexity, you can achieve better result using Selection algorithm .如果您担心复杂性,则可以使用Selection algorithm获得更好的结果。 Using it you can perform the operations in O(n) complexity.使用它,您可以执行O(n)复杂度的操作。

You can also try this -你也可以试试这个——

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class secondHighestLowest : System.Web.UI.Page
{
    int[] arr = new int[10] { 45, 3, 64, 6, 24, 75, 3, 6, 24, 45 };

    protected void Page_Load(object sender, EventArgs e)
    {
        secondHighestLowestNumber();
        secoundLowestNumber();
    }

    private void secondHighestLowestNumber()
    {
        int firstHighestNumber = arr[0];
        int secondHighestNumber = arr[0];
        for(int i = 0; i<arr.Length; i++)
        {
            if (arr[i]>firstHighestNumber)
            {
                firstHighestNumber = arr[i];
            }
        }

        for (int x = 0; x < arr.Length; x++)
        {
            if (arr[x]>secondHighestNumber && firstHighestNumber!=arr[x])
            {
                secondHighestNumber = arr[x];
            }
        }

        Response.Write("secondHighestNumber---- " + secondHighestNumber + "</br>");
    }

    private void secoundLowestNumber()
    {
        int firstLowestNumber = arr[0];
        int secondLowestNumber = arr[0];
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] < firstLowestNumber)
            {
                firstLowestNumber = arr[i];
            }
        }

        for (int x = 0; x < arr.Length; x++)
        {
            if (arr[x] < secondLowestNumber && firstLowestNumber != arr[x])
            {
                secondLowestNumber = arr[x];
            }
        }

        Response.Write("secondLowestNumber---- " + secondLowestNumber + "</br>");
    }
}

Hope this is helpful :)希望这有帮助:)

        int[] i = new int[] { 4, 8, 1, 9, 2, 7, 3 };
        Array.Sort(i);
        Console.WriteLine("Highest number :" + i[i.Length-1]);
        Console.WriteLine("Second highest number :"+i[i.Length-2]);
        Console.WriteLine("Lowest number :" + i[i.Length-i.Length]);
        Console.WriteLine("Second Lowest number :" + i[i.Length -i.Length+1]);

        Output : Highest number : 9

                 Second highest number : 8

                 Lowest number : 1

                 Second Lowest number : 2

Why two loops when can be done in为什么可以在其中完成两个循环

        int[] myArray = new int[] { 2, 4, 3, 6, 9 };

        int max1 = 0;
        int max2 = 0;

        for (int i = 0; i < myArray.Length; i++)
        {
            if (myArray[i] > max1)
            {
                max2 = max1;
                max1 = myArray[i];

            }
            else
            {
                max2 = myArray[i];
            }
        }

        Console.WriteLine("first" + max1.ToString());
        Console.WriteLine("Second" + max2.ToString());
        Console.ReadKey();

I have first arranged them using Selection Sort algorithm in ascending order, then i displayed values.我首先使用选择排序算法按升序排列它们,然后显示值。

static void Main(string[] args)
    {
        int[] Num =new int[] { 3, 4, 5, 6, 7, 0,99,105,55 };

        int temp;
        for(int a=0;a<Num.Length-1;a++)
        {
            for(int b=a+1;b<Num.Length;b++)
            {
                if(Num[a]>Num[b])
                {
                    temp = Num[a];
                    Num[a] = Num[b];
                    Num[b] = temp;
                }
            }

        }


        Console.WriteLine("Max value ="+Num[Num.Length-1]+"\nSecond largest Max value="+ Num[Num.Length - 2]+"\nMin value =" + Num[0] + "\nSecond smallest Min value=" + Num[1]);
        Console.WriteLine("\nPress to close");
        Console.ReadLine();
    }

This algorithm is applicable even with duplicate record.即使有重复记录,该算法也适用。

int[] intArrayInput= new int[] { 855, 3, 64, 6, 24, 75, 3, 6, 24, 45, 855 };

Using Sort:使用排序:

Array.Sort(intArrayInput);
int intMax = intArrayInput[intInput.Length - 1];
int intLow = intArrayInput[0];

And using LINQ:并使用 LINQ:

int intSecondMax = intArrayInput.OrderByDescending(x => x).Distinct().Skip(1).First();
int intSecondLow = intArrayInput.OrderBy(x => x).Distinct().Skip(1).First();

Here is an easy solution assuming you array has at least two members.这是一个简单的解决方案,假设您的数组至少有两个成员。

For the lowest number in the array.对于数组中的最低数字。

 Array.Sort(new); string newNumber = string.Join(" ", new[0]); return newNumber;

For second lowest number in the array.对于数组中第二小的数字。

 Array.Sort(new); string newNumber = string.Join(" ", new[1]); return newNumber;

for the highest number in the array.为数组中的最高数字。

 Array.Sort(new); string newNumber = string.Join(" ", new[new.Length - 1]); return newNumber;

For the second highest number in the array.对于数组中的第二大数字。

 Array.Sort(new); string newNumber = string.Join(" ", new[new.Length - 1]); return newNumber;

Shortest way to find out largest, second largest, smallest & second smallest number, using conventional method:使用常规方法找出最大、第二大、最小和第二小的数字的最短方法:

int LargestNumber = 0;
int SecondLargestNumber = 0;
int SmallestNumber = 0;
int SecondSmallestNumber = 0;
int[] array = new int[] { 250, 5, 17, 50, 98, 352, 2, 8, 67, 150, 200, 1 };

for (int i = 0; i < array.Length; i++)
{
    if (array[i] > LargestNumber)
    {
        SecondLargestNumber = LargestNumber;
        LargestNumber = array[i];
    }
    else if (array[i] > SecondLargestNumber)
    {
        SecondLargestNumber = array[i];
    }

    if (i == 0)
    {
        SmallestNumber = array[0];
        SecondSmallestNumber = array[1];
    }

    if (array[i] < SmallestNumber)
    {
        SecondSmallestNumber = SmallestNumber;
        SmallestNumber = array[i];
    }
    else if (array[i] < SecondSmallestNumber)
    {
        SecondSmallestNumber = array[i];
    }
}

Console.WriteLine("Largest Number : " + LargestNumber);
Console.WriteLine("Second Largest Number : " + SecondLargestNumber);
Console.WriteLine("Smallest Number : " + SmallestNumber);
Console.WriteLine("Second Smallest Number : " + SecondSmallestNumber);
int[] myUnSortArray = { 1, 5, 8, 3, 10, 6, 19, 5, 4, 4 };

int[] SortedArray = (from number in myUnSortArray
                     orderby number ascending
                     select number).ToArray();

int highestValue = SortedArray.Max();
int SecondHighest = SortedArray.Last(m => m < highestValue);

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

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