- 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:
- Given an input array {2, 4, 6, 8, 10, 12}.
- Largest element in an array is 12 and second largest element in an array is 10.
Example 2:
- Given an input array {5, 3, 4, 1, 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
- Declare largest element as first element of array ( firstLargest = inputArray[0]).
- Declare second largest element as minimum integer value (i.e. Integer.MIN_VALUE).
- Start traversing through the array of integers.
- If current array element is greater than largest element (or first largest element).
- We should update first largest & second largest element.
- Update second largest element by first largest element.
- Update first largest element by current array element.
- If current element lies between firstLargest & secondLargest.
- i.e. secondLargest < currentElement && firstLargest > currentElement
- Update secondLargest by currentElement.
- At end of loop, we will get second largest element in an array of integers.
- 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