In this article, you will see how to find Second largest number in an Array using Java. To understand this program, you should have the basic knowledge of an integer array and the looping concept. Let’s see a few examples here,
Example #1. Find the 2nd largest number in the array using java
In this example, we sort the array in ascending order, and after that, we get the 2nd last index value as the largest number. Let’s see the java code below:
package com.javacodepoint.array;
public class SecondLargestNumberInArrayExample1 {
// Method to find second largest number
public static int findSecondLargestNo(int arr[]) {
// Sorting the array element in ascending order
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// 2nd last element will be the second largest number
return arr[arr.length - 2];
}
// Main method
public static void main(String[] args) {
// Declare and Initialize an integer Array
int arr[] = { 5, 8, 12, 8, -5, 0, 4, 22, 7, 11 };
int secondMax = findSecondLargestNo(arr);
// Print the second largest number
System.out.println("The Second Largest Number in the Array: " + secondMax);
}
}
OUTPUT:
The Second Largest Number in the Array: 12
Example #2. Find the second largest number in an array java using Scanner
In this example, we initialize the integer array from the user-given value using the Scanner class, and the rest are similar to example 1. Let’s have a look at the java code for it below:
package com.javacodepoint.array;
import java.util.Scanner;
public class SecondLargestNumberInArrayExample2 {
// Method to find second largest number
public static int findSecondLargestNo(int arr[]) {
// Sorting the array element in ascending order
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// 2nd last element will be the second largest number
return arr[arr.length - 2];
}
// Main method
public static void main(String[] args) {
// Creating scanner object for reading user inputs
Scanner sc = new Scanner(System.in);
// Read the size of the Array
System.out.println("Enter the size of the Array: ");
int n = sc.nextInt();
// Declare the integer Array of given size
int arr[] = new int[n];
// Reading the Array values
System.out.println("Enter " + n + " element(s) of the Array: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int secondMax = findSecondLargestNo(arr);
// Print the second largest number
System.out.println("The Second Largest Number in the Array: " + secondMax);
}
}
OUTPUT:
Enter the size of the Array:
5
Enter 5 element(s) of the Array:
2
4
8
1
15
The Second Largest Number in the Array: 8
Example #3. Find the second largest number in the array without sorting
In this example, we will find the second-largest number in an array without using any sorting technique. Let’s see the java code below:
package com.javacodepoint.array;
public class SecondLargestNumberInArrayExample3 {
// Method to find second largest number
public static int findSecondLargestNo(int arr[]) throws Exception {
// Array length
int n = arr.length;
if (n < 2) {
throw new Exception("Array length should be at least 2");
}
// Initialize max and secondMax
int max = arr[0];
int secondMax = arr[1];
// Swap the number if max < secondMax
if (max < secondMax) {
int temp = max;
max = secondMax;
secondMax = temp;
}
// Iterate the array from index 2 to array length
for (int i = 2; i < arr.length; i++) {
if (arr[i] > max) {
secondMax = max;
max = arr[i];
} else if (arr[i] > secondMax) {
secondMax = arr[i];
}
}
return secondMax;
}
// Main method
public static void main(String[] args) throws Exception {
// Declare and Initialize an integer Array
int arr[] = { 5, 8, 12, 8, -5, 0, 4, 22, 7, 11 };
int secondMax = findSecondLargestNo(arr);
// Print the second largest number
System.out.println("The Second Largest Number in the Array: " + secondMax);
}
}
OUTPUT:
The Second Largest Number in the Array: 12
Example #4. Find the 2nd large number using Collections in java
In this example, we use the collections to find the second-largest number in java. Let’s see the code below:
package com.javacodepoint.array;
import java.util.Arrays;
public class SecondLargestNumberInArrayExample4 {
// Method to find second largest number
public static int findSecondLargestNo(int arr[]) {
// Sort the array
Arrays.sort(arr);
// Second largest number
return arr[arr.length - 2];
}
// Main method
public static void main(String[] args) {
// Declare and Initialize an integer Array
int arr[] = { 5, 8, 12, 15, 4, 22, 7, 11 };
int secondMax = findSecondLargestNo(arr);
// Print the second largest number
System.out.println("The Second Largest Number in the Array: " + secondMax);
}
}
OUTPUT:
The Second Largest Number in the Array: 15
See also: Java Program to find the Smallest Number in an Array.