Find Second Largest Number in Array Java

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.

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!

1 thought on “Find Second Largest Number in Array Java”

  1. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *