Introduction
Finding the second largest number in an array is a common problem that helps you practice array manipulation and sorting techniques. This guide will walk you through writing a Java program that identifies the second largest number in a given array.
Problem Statement
Create a Java program that:
- Prompts the user to enter the size of an array and its elements.
- Finds the second largest number in the array.
- Displays the second largest number.
Example:
- Input:
[3, 5, 7, 2, 8] - Output:
"The second largest number is 7"
Solution Steps
- Read the Array Size and Elements: Use the
Scannerclass to take the size and elements of the array as input from the user. - Find the Second Largest Number: Traverse the array to find the largest and second largest numbers.
- Display the Result: Print the second largest number.
Java Program
// Java Program to Find the Second Largest Number in an Array
// Author: https://www.rameshfadatare.com/
import java.util.Scanner;
public class SecondLargestInArray {
public static void main(String[] args) {
// Step 1: Read the size and elements of the array from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
int[] array = new int[size];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
// Step 2: Find the second largest number
int largest = Integer.MIN_VALUE;
int secondLargest = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
if (array[i] > largest) {
secondLargest = largest;
largest = array[i];
} else if (array[i] > secondLargest && array[i] != largest) {
secondLargest = array[i];
}
}
// Step 3: Display the result
if (secondLargest == Integer.MIN_VALUE) {
System.out.println("There is no second largest number in the array.");
} else {
System.out.println("The second largest number is " + secondLargest);
}
}
}
Explanation
Step 1: Read the Array Size and Elements
- The
Scannerclass is used to read the size of the array and its elements. ThenextInt()method captures the size and each element.
Step 2: Find the Second Largest Number
- Initialize two variables
largestandsecondLargesttoInteger.MIN_VALUE, which is the smallest possible value an integer can have. This is to ensure that any number in the array will be larger. - Traverse the array:
- If the current element is greater than
largest, updatesecondLargesttolargest, and updatelargestto the current element. - If the current element is greater than
secondLargestbut not equal tolargest, updatesecondLargestto the current element.
- If the current element is greater than
Step 3: Display the Result
- If
secondLargestremainsInteger.MIN_VALUE, it indicates that there is no distinct second largest number (e.g., all elements are the same or there is only one element). Otherwise, the program prints the second largest number.
Output Example
Example 1:
Enter the size of the array: 5
Enter the elements of the array:
3 5 7 2 8
The second largest number is 7
Example 2:
Enter the size of the array: 4
Enter the elements of the array:
6 6 6 6
There is no second largest number in the array.
Example 3:
Enter the size of the array: 3
Enter the elements of the array:
10 20 20
The second largest number is 10
Conclusion
This Java program demonstrates how to find the second largest number in an array. It covers essential concepts such as array traversal, conditional logic, and handling edge cases, making it a valuable exercise for beginners learning Java programming.