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 a set of random numbers in C#

2 Answers

0 votes
using System;

public class BeforeMax
{
    public static int FindSecondMax(int total, int maxValue) {
        Random rnd = new Random();
        int max = rnd.Next(1, maxValue);
        int beforeMax = max;

        Console.WriteLine(max);

        for (int i = 1; i < total; i++) {
            int n = rnd.Next(1, maxValue);
            Console.WriteLine(n);

            if (n > max) {
                beforeMax = max;
                max = n;
            }
            else if (n > beforeMax) {
                beforeMax = n;
            }
        }

        Console.WriteLine("max = " + max);
        return beforeMax;
    }

    public static void Main(string[] args)
    {
        int secondMax = FindSecondMax(15, 100);
        
        Console.WriteLine("before_max = " + secondMax);
    }
}

  
  
/*
run:
  
1
30
18
85
96
49
47
2
75
72
52
26
95
27
48
max = 96
before_max = 95
  
*/


answered Jul 15, 2014 by avibootz
edited Oct 4, 2025 by avibootz
0 votes
using System;

class BeforeMax
{
    // Function to find the second largest number
    static int FindSecondLargest(int[] numbers)
    {
        int largest = int.MinValue;
        int secondLargest = int.MinValue;

        foreach (int number in numbers) {
            if (number > largest) {
                secondLargest = largest; // Update second largest
                largest = number;        // Update largest
            }
            else if (number > secondLargest && number != largest) {
                secondLargest = number;
            }
        }

        return secondLargest;
    }

    static void Main()
    {
        // Generate a set of random numbers
        Random random = new Random();
        int[] numbers = new int[10];
        for (int i = 0; i < numbers.Length; i++) {
            numbers[i] = random.Next(1, 101); // Random numbers between 1 and 100
        }

        // Display the random numbers
        Console.WriteLine("Numbers: " + string.Join(", ", numbers));

        // Call the function to find the second largest number
        int secondLargest = FindSecondLargest(numbers);

        if (secondLargest == int.MinValue) {
            Console.WriteLine("The array does not have enough unique numbers to determine the second largest.");
        }
        else {
            Console.WriteLine("Second Largest: " + secondLargest);
        }
    }
}

 
/*
run:
 
Numbers: 77, 82, 100, 24, 49, 14, 55, 53, 1, 42
Second Largest: 82
 
*/

 



answered Apr 5, 2025 by avibootz
...