Java Program to Find the Second Largest Number in an Array

1. Introduction

Finding the second largest number in an array is a common algorithmic challenge that tests a programmer's ability to work with arrays and sorting. It's a variation of the problem of finding the largest or smallest number in a collection of data. This blog post will demonstrate a Java program to find the second largest number in an array without sorting the entire array, which can be more efficient for large datasets.

2. Program Steps

1. Declare and initialize an array with numeric values.

2. Set the first number as the largest and the second number as the second largest or vice versa based on their values.

3. Loop through the array starting from the third element, comparing each element with the largest and second largest numbers.

4. Update the largest and second largest numbers based on the comparisons.

5. Display the second largest number after traversing the entire array.

3. Code Program

public class SecondLargestInArray {
    public static void main(String[] args) {
        // Initializing an array with values
        int[] numbers = {45, 86, 63, 29, 57, 90, 83, 39};

        // Initializing the largest and second largest numbers
        int largest = Integer.MIN_VALUE;
        int secondLargest = Integer.MIN_VALUE;

        // Looping through the array to find the largest and second largest numbers
        for (int number : numbers) {
            if (number > largest) {
                secondLargest = largest; // Update second largest
                largest = number; // Update largest
            } else if (number > secondLargest && number != largest) {
                secondLargest = number; // Update second largest if it's different from the largest
            }
        }

        // Displaying the second largest number
        System.out.println("The second largest number is: " + secondLargest);
    }
}

Output:

The second largest number is: 86

Explanation:

1. The program starts by initializing an array numbers with a set of integer values.

2. It then initializes two variables, largest and secondLargest, with the smallest possible integer value (Integer.MIN_VALUE). This is done to ensure that any number in the array will be larger than these initial values.

3. The program iterates over each number in the array using an enhanced for loop. For each number, it checks if it's larger than the current largest number. If so, the current secondLargest number is updated to the value of largest, and largest is updated to the current number.

4. If the current number is not larger than largest but is larger than secondLargest, and is not equal to largest, secondLargest is updated to the current number. This condition ensures that the second largest number is distinct from the largest number, considering the case where the largest number might appear more than once in the array.

5. After the loop completes, the program prints the value of secondLargest, which now holds the second largest number in the array.

Comments