Java program to find second largest number in an array



Step 1:

    Iterate the given array

Step 2 (first if condition arr[i] > largest):

    If current array value is greater than largest value then

        Move the largest value to secondLargest and make

        current value as largest

Step 3 (second if condition arr[i] > secondLargest )  

  If the current value is smaller than largest and greater than secondLargest  then

        the current value becomes secondLargest


Program

package com.candidjava;

public class SecondLargest {

	public static void main(String[] args) {

		int arr[] = { 14, 46, 47, 86, 92, 52, 48, 36, 66, 85 };
		int largest = arr[0];
		int secondLargest = arr[0];
		
		System.out.println("The given array is:" );
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i]+"\t");
		}
		for (int i = 0; i < arr.length; i++) {

			if (arr[i] > largest) {
				secondLargest = largest;
				largest = arr[i];

			} else if (arr[i] > secondLargest) {
				secondLargest = arr[i];

			}
		}

		System.out.println("\nSecond largest number is:" + secondLargest);

	}
}

Output

The given array is:

14 46 47 86 92 52 48 36 66 85

Second largest number is:86


Related example

        Java program to find largest number in an array
        Java program to find second largest number in an array
        Java program to find largest and smallest number in an array
        Java program to find largest and second largest number in an array
        Java program to find second smallest number in an array
        Java program to find index of max value in array java
        Java program to find index of smallest element in array java



Related Post

Comments

Sudha
         This method fails when your first element in the array is bigger. E.g. {234,54,200,123,6} In order to avoid the failure Initialize Largest and secondLargest Valriables to it's default value i.e. 0. As below : int largest =0, int secondLargest=0; Rest of the code remains same. Happy coding!
rajkumar pawar
         or u can do initialize largest with a[0] and second last with a[1] and start the for loop from i=1;cheers!!
Tanveer Singh
         What if all the elements are the same.
Anonymous
         This algorithm doesn't work for the situation where the largest number occurs in the first element of the array. If you swap 14 with 92 (i.e. input { 92, 46, 47, 86, 14, 52, 48, 36, 66, 85 }), the result would be 92.
Anonymous
         reply Sudha: what if the input is {-1,-2,-3,-4,-5}?
Nagaraj
         This code doesn't work if there are duplicate highest numbers in the array for example int arr[] = { 14, 46, 47, 86, 92, 52, 48, 36, 66, 85, -99, -88,92 };
Vikram
         Try this ! public class SecondLargestElement { @Test public void testSecondLargest() { // int arr[] = { 91,92,93,94,95,96,97,100 }; int arr[] = { 91,91,92 }; findSecondLargest(arr); } private void findSecondLargest(int [] numbers) { int size = numbers.length; int largest = Integer.MIN_VALUE; int secondLargest = Integer.MIN_VALUE; if (size<2) { System.out.println("size is less than 2"); return; } for (int i = 0;ilargest) { secondLargest = largest; largest = numbers[i]; } else if (numbers[i]>secondLargest && numbers[i]!=largest) { secondLargest = numbers[i]; } } if (secondLargest == Integer.MIN_VALUE) { System.out.println("second largest number is not present "); } else { System.out.println("second largest is "+secondLargest); } } }

©candidjava.com