Write a java program to find second largest number in an integer
array?
This is also one of
the java interview program asked many a times to java freshers to check the
candidate’s logical ability and understanding of the language’s fundamentals.
While writing the program you should not use any sorting methods or any
collection types. You should find the second largest number in the
given array by iterating the array only once. These are the likely
conditions interviewer may ask you to follow. In this article, we will try
to find second largest number in an integer array using iterative method.
Logic Used To Find Second Largest Number In An Integer Array :
First, we compare
the first two elements of the given array. The largest among these two
elements will be assigned to ‘firstLargest‘ and smallest will be assigned to ‘secondLargest‘.
We iterate the remaining elements through for loop. If
any element of the remaining elements is greater than the ‘firstLargest‘,
then we assign that element to ‘firstLargest‘ and current value of ‘firstLargest‘
will be assigned to ‘secondLargest‘. If any element is smaller than ‘firstLargest‘
and greater than ‘secondLargest‘, then we assign that element
to ‘secondLargest‘.
Java Program To Find
Second Largest Number In An Integer Array :
Below is the complete
java code to find second largest element in an integer array.public class MainClass {
static int secondLargest(int[] input) {
int firstLargest, secondLargest;
// Checking first two elements of input array
if (input[0] > input[1]) {
// If first element is greater than second element
firstLargest = input[0];
secondLargest = input[1];
} else {
// If second element is greater than first element
firstLargest = input[1];
secondLargest = input[0];
}
// Checking remaining elements of input array
for (int i = 2; i < input.length; i++) {
if (input[i] > firstLargest) {
// If element at 'i' is greater than 'firstLargest'
secondLargest = firstLargest;
firstLargest = input[i];
} else if (input[i] < firstLargest && input[i] > secondLargest) {
// If element at 'i' is smaller than 'firstLargest' and greater
// than 'secondLargest'
secondLargest = input[i];
}
}
return secondLargest;
}
public static void main(String[] args) {
System.out.println(secondLargest(new int[] { 45, 51, 28, 75, 49, 42 }));
System.out.println(secondLargest(new int[] { 985, 521, 975, 831, 479, 861 }));
System.out.println(secondLargest(new int[] { 9459, 9575, 5692, 1305, 1942, 9012 }));
System.out.println(secondLargest(new int[] { 47498, 14526, 74562, 42681, 75283, 45796 }));
}
}
Output :
51975
9459
74562
EmoticonEmoticon