How to get the second highest number in an array in Visual C#?

后端 未结 9 1794
误落风尘
误落风尘 2020-12-17 05:40

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?

9条回答
  •  佛祖请我去吃肉
    2020-12-17 05:49

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

提交回复
热议问题