# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Find Second Largest Number in Array Java | Array Programs in Java – 14 | In the previous Java program, we developed programs to find the largest number in an array and find the smallest number in an array. Now, let us see how to find the second largest number in array Java. Or, how to find the second max value in array Java?
Program description:- Write a Java program to find the second largest number in Java.
Procedure to develop the method to find the second largest number in Java Array,
a) Take a one-dimensional array (assume array variable as arr)
b) Declare a variable fmax and smax
c) Assign first element of the array to both variable i.e. fmax = arr[0]; smax = arr[0];
d) Iterate through all elements of the array using the loop
e) Check the ith element in the array is greater than fmax?
f) If yes, then update smax = fmax variable and fmax = arr[i]
g) Else if, the ith element in the array is greater than smax?
h) Then update smax = arr[i]
i) Repeat (e) to (h) until the end of the array.
j) Return smax
Method to Find Second Largest Number in Array Java
// Java method to find second largest
// number in array
public static int secondlargest(int[] arr) {
// declare variables
int fmax = 0; // first largest
int smax = 0; // second largest
// assign first element to fmax, smax
fmax = arr[0];
smax = arr[0];
// compare with remaining elements
for (int i = 1; i < arr.length; i++) {
if (fmax < arr[i]) {
smax = fmax;
fmax = arr[i];
} else if(smax < arr[i]) {
smax = arr[i];
}
}
// return second largest number
return smax;
}
At the start, we assume that the first element of the array is the first and second-largest number and then compare it with the remaining elements. If any element is larger than the first largest number, then the previous largest number becomes a second-largest number and the current number becomes the first largest number. Else if the current element is only greater than the second-largest number not greater than the first largest number then the current element will be the second-largest number. Continue this process till the end of the array.
Using the above method let us develop a Java program to find the second largest number in array Java. We will take array input from the end-user, first, we will ask to enter the size of the array and then enter the array elements.
To take input from the end-user we will use the Scanner class, but you can also use the BufferedReader class. After finding the second largest number in the given array we will display the result.
Java Program to Find the Largest or Max Number in Array
Program to find the second largest number in array Java by taking input values from the end-user.
import java.util.Scanner;
public class ArrayProgram {
// method to find second largest number in array
public static int secondLargest(int[] arr) {
// declare variables
int fmax = 0;
int smax = 0;
// assign first element to fmax, smax
fmax = arr[0];
smax = arr[0];
for (int i = 1; i < arr.length; i++) {
if (fmax < arr[i]) {
smax = fmax;
fmax = arr[i];
} else if(smax < arr[i]) {
smax = arr[i];
}
}
return smax;
}
// main method
public static void main(String[] args) {
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// declare variables
int length = 0;
int numbers[] = null;
// take length of the array
System.out.print("Enter length of the array: ");
length = scan.nextInt();
// create array
numbers = new int[length];
// take array inputs
System.out.println("Enter array elements: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scan.nextInt();
}
// method call
System.out.println("Second largest element = "
+ secondLargest(numbers));
// close Scanner
scan.close();
}
}
Output for the different test-cases:-
Enter length of the array: 5
Enter array elements:
25 10 35 15 45
Second largest element = 35
Enter length of the array: 5
Enter array elements:
-30 -50 10 -20 -35
Second largest element = -20
In this program to find the second largest number in array Java, first, we created a Scanner class object to get input values from the end-user. Then, the length of the array and array elements are aksed from the end-user and stored into appropriate variables. On this array, we called the secondLargest() method.
Note:- Logic written for finding the second largest number from the array will not work if the array contains duplicate elements. So we need to first remove duplicates from the array then we need to sort that array in order to find out the correct second-largest number.
Find the Second Largest Number in the Array Using Stream
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] numbers = { 10, 5, -15, 20, -30 };
int secondLargest = Arrays.stream(numbers)
.sorted() // sort the numbers
.skip(1) // skip first element in sorted numbers
.limit(1) // take only 1 element from resultant
.findFirst().getAsInt();
System.out.println(secondLargest); // -15
}
}
In the case of the list we can do this as follows:-
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> numbers = List.of(10, 5, -15, 20, -30);
Integer secondLargest = numbers.stream()
.sorted()
.skip(1)
.limit(1)
.findFirst().get();
System.out.println(secondLargest); // -15
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Logic written for finding the second largest number from the array will not work if the array contains duplicate elements. So we need to first remove duplicates from the array then we need to sort that array in order to find out the correct second-largest number.