Find second largest element in an array of integers (java / example)

  • Given an array of integers in java.
  • Print second largest element in an array (in single iteration).

Example – find second largest element in an array.

Example 1:

  1. Given an input array  {2, 4, 6, 8, 10, 12}.
  2. Largest element in an array is 12 and second largest element in an array is 10.

Example 2:

  1. Given an input array  {5, 3, 4, 1, 2}.
  2. Largest element in an array is 5 and second largest element in an array is 4.

Example 3:

  • Given an input array  {16, 41, 41, 9, 41, 7}.
  • Largest element in an array is 41 and second largest element in an array is 16.

Algorithm – find second largest element in an array of integers

  1. Declare largest element as first element of array ( firstLargest = inputArray[0]).
    • Declare second largest element as minimum integer value (i.e. Integer.MIN_VALUE).
  2.  Start traversing through the array of integers.
    1. If current array element is greater than largest element (or first largest element).
      1. We should update first largest & second largest element.
      2. Update second largest element by first largest element.
      3. Update first largest element by current array element.
    2. If current element lies between firstLargest & secondLargest.
      1. i.e. secondLargest < currentElement && firstLargest > currentElement
      2. Update secondLargest by currentElement.
  3. At end of loop, we will get second largest element in an array of integers.
  4. Time complexity of algorithm is O (n).

Program – find second largest element from array of integers in java

SecondLargestElement class:

  • SecondLargestElement class is responsible for finding the second largest element in an array of integers.
  • printSecondLargestElement method will print second largest element.
package org.learn;

import java.util.Arrays;

public class SecondLargestElement {

    public static void main(String[] args) {
        int[] inputArray = {2, 4, 6, 8, 10, 12};
        printSecondLargestElement(inputArray);

        System.out.println();
        inputArray = new int[]{5, 3, 4, 1, 2};
        printSecondLargestElement(inputArray);

        System.out.println();
        inputArray = new int[]{16, 41, 41, 9, 41, 7};
        printSecondLargestElement(inputArray);
    }

    private static void printSecondLargestElement(int[] inputArray) {
        int firstLargest = inputArray[0];
        int secondLargest = Integer.MIN_VALUE;

        for (int index = 1; index < inputArray.length; index++) {
            int currentElement = inputArray[index];
            if (firstLargest < currentElement) {
                secondLargest = firstLargest;
                firstLargest = currentElement;
            } else if (secondLargest < currentElement && firstLargest > currentElement) {
                secondLargest = currentElement;
            }
        }

        String array = Arrays.toString(inputArray);
        if (secondLargest != Integer.MIN_VALUE) {
            System.out.printf("Second Largest element in %s is : %d", array, secondLargest);
        } else {
            System.out.printf("There is not second largest element in array %s ", array);
        }
    }
}

Output – second largest element in an array of integers in java

Second Largest element in [2, 4, 6, 8, 10, 12] is : 10
Second Largest element in [5, 3, 4, 1, 2] is : 4
Second Largest element in [16, 41, 41, 9, 41, 7] is : 16
Scroll to Top