Find Second Largest Number in Array Java

Find Second Largest Number in Array Java | Find 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

Java Method to Find the Second Largest Number in Array

// 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
  // loop
  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 the Java program. We will take 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

import java.util.Scanner;

public class ArrayProgram {

  // 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
    // loop
    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 result
    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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply