Opening Hours :7AM to 9PM
class SecondLargest { public int findSecondLargest(int[] arr, int n) { int firstLargest = Integer.MIN_VALUE; int secondLargest = Integer.MIN_VALUE; for (int i = 0; i < n; i++) { if (arr[i] > firstLargest) { secondLargest = firstLargest; firstLargest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != firstLargest) { secondLargest = arr[i]; } } return secondLargest; } } public class Main { public static void main(String[] args) { SecondLargest sl=new SecondLargest(); int[] arr = {1, 14, 2, 16, 10, 20, 5}; int n = arr.length; int secondLargest = sl.findSecondLargest(arr, n); System.out.println("Second largest element is " + secondLargest); } }In this program, The findSecondLargest method takes two arguments; the array, and the length of the array.