Type-1: 

Here's a Java program to find the second-largest element in an array of integers:


public class SecondLargestElement
public static void main(String[] args) {
// Step 1: Define an integer array 
int[] numbers = { 10, 20, 5, 30, 15 };
// Step 2: Call the function to find the second-largest element 
int secondLargest = findSecondLargest(numbers); 
// Step 3: Print the second-largest element
System.out.println("Second Largest Element: " + secondLargest); 
 } 
// Function to find the second-largest element in the array 
public static int findSecondLargest(int[] arr)
// Step 4: Initialize variables to track the largest and second-largest elements
int largest = Integer.MIN_VALUE; 
int secondLargest = Integer.MIN_VALUE;
// Step 5: Iterate through the array to find the largest and second-largest elements 
for (int number : arr) { 
if (number > largest) { 
 secondLargest = largest; 
 largest = number; 
 }
else if (number > secondLargest && number != largest) {
 secondLargest = number; 
 }
 }
// Step 6: Return the second-largest element 
return secondLargest; }
 }

Explanation of each step:

  1. We start by defining an integer array named numbers. In this example, we have an array with elements {10, 20, 5, 30, 15}.
  2. We call the function findSecondLargest(numbers) to find the second-largest element in the array.
  3. We then print the second-largest element.
  4. The function findSecondLargest takes an integer array arr as input and returns the second-largest element in the array.
  5. We initialize two variables largest and secondLargest to track the largest and second-largest elements in the array. We set both of these variables to the minimum possible integer value at the beginning (Integer.MIN_VALUE) to handle cases where all elements in the array are negative.
  6. We use a for-each loop to iterate through the array. For each element number in the array, we compare it with the current largest. If number is greater than largest, we update both largest and secondLargest. If number is not greater than largest but is greater than secondLargest and not equal to largest, we update secondLargest.
  7. After the loop, secondLargest will hold the value of the second-largest element in the array.
  8. Finally, we return the value of secondLargest.


When you run this program with the provided array, it will output:

Second Largest Element: 20

This is because the second-largest element in the array {10, 20, 5, 30, 15} is 20. If you modify the array elements, the program will find and display the new second-largest element accordingly.

Type-2:

Here's an alternative Java program to find the second-largest element in an array of integers without using any auxiliary variables:


public class SecondLargestElement {
public static void main(String[] args) {
// Step 1: Define an integer array
int[] numbers = { 10, 20, 5, 30, 15 };
// Step 2: Call the function to find the second-largest element
int secondLargest = findSecondLargest(numbers);
// Step 3: Print the second-largest element 
System.out.println("Second Largest Element: " + secondLargest); 
 }
// Function to find the second-largest element in the array 
public static int findSecondLargest(int[] arr)
// Step 4: Handle the case when the array has less than two elements
if (arr.length < 2) {
throw new IllegalArgumentException("Array must have at least two elements.");
 } 
// Step 5: Find the largest element in the array
int largest = Math.max(arr[0], arr[1]); 
int secondLargest = Math.min(arr[0], arr[1]); 
// Step 6: Iterate through the array to find the second-largest element 
for (int i = 2; i < arr.length; i++) { 
if (arr[i] > largest) {
 secondLargest = largest; largest = arr[i];
 }
else if (arr[i] > secondLargest && arr[i] != largest) {
 secondLargest = arr[i]; 
 }
 } 
// Step 7: Return the second-largest element 
return secondLargest; } }

Explanation of the alternative method:

  1. We start by defining an integer array named numbers, just like in the previous example.
  2. We call the function findSecondLargest(numbers) to find the second-largest element in the array.
  3. We then print the second-largest element.
  4. The function findSecondLargest takes an integer array arr as input and returns the second-largest element in the array.
  5. We handle the special case where the array has less than two elements. In such a case, there is no second-largest element, so we throw an IllegalArgumentException.
  6. Instead of using explicit variables to keep track of largest and secondLargest, we use the Math.max() and Math.min() functions to find the initial values of these variables.
  7. We iterate through the array, starting from the third element (i = 2). We compare each element with the current largest and secondLargest, updating them accordingly.
  8. After the loop, secondLargest will hold the value of the second-largest element in the array.
  9. Finally, we return the value of secondLargest.


When you run this program with the provided array, it will output:

Second Largest Element: 20

This is because the second-largest element in the array {10, 20, 5, 30, 15} is 20. If you modify the array elements, the program will find and display the new second-largest element accordingly.