Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to find the second biggest number in array in C#

3 Answers

0 votes
using System;

public class SecondBiggestNumberArray
{
    public static void SetRandomNumbersInArray(int[] arr) {
        Random rnd = new Random();
        int size = arr.Length;
        
        for (int i = 0; i < size; i++) {
            arr[i] = rnd.Next(1, 100);
            Console.Write("{0} ", arr[i]);
        } 
    }

    public static int FindSecondBiggestNumberArray(int[] arr) {
        int max, before_max;
        int size = arr.Length;
        
        max = before_max = int.MinValue;
        
        for (int i = 0; i < size; i++) {
            if (arr[i] > max) {
                before_max = max;
                max = arr[i];
            }
            else {
                if (arr[i] > before_max) {
                    before_max = arr[i];
                }
            }
        }
        
        return before_max;
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[10];
 
        SetRandomNumbersInArray(arr);
        
        Console.WriteLine("\nSecond Biggest Number = {0}", FindSecondBiggestNumberArray(arr));
    }
}



/*
run:

90 94 70 20 41 76 74 27 60 26 
Second Biggest Number = 90

*/


answered Apr 10, 2014 by avibootz
edited Jan 19, 2025 by avibootz
0 votes
using System;

public class SecondBiggestNumberArray
{
    public static void SetRandomNumbersInArray(int[] arr) {
        Random rnd = new Random();
        int size = arr.Length;
        
        for (int i = 0; i < size; i++) {
            arr[i] = rnd.Next(1, 100);
            Console.Write("{0} ", arr[i]);
        } 
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[10];
 
        SetRandomNumbersInArray(arr);
        
        Array.Sort(arr);

        Console.WriteLine("\nSecond Biggest Number = {0}", arr[arr.Length - 2]);
    }
}



/*
run:

30 40 78 66 30 94 63 93 72 65 
Second Biggest Number = 93


*/

 



answered Jan 19, 2025 by avibootz
0 votes
using System;
using System.Linq;

public class SecondBiggestNumberArray
{
    public static void SetRandomNumbersInArray(int[] arr) {
        Random rnd = new Random();
        int size = arr.Length;
        
        for (int i = 0; i < size; i++) {
            arr[i] = rnd.Next(1, 100);
            Console.Write("{0} ", arr[i]);
        } 
    }

    public static void Main(string[] args)
    {
        int[] arr = new int[10];
 
        SetRandomNumbersInArray(arr);

        int secondLargest = arr.OrderByDescending(n => n).Distinct().Skip(1).First();
        
        Console.WriteLine("\nThe second largest number is: " + secondLargest);
    }
}



/*
run:

85 77 22 28 1 96 95 87 57 71 
The second largest number is: 95


*/

 



answered Jan 19, 2025 by avibootz
...