Write a Program to find second largest number in an array

Write a Program to find second largest number in an array

Write a Program to find second largest number in an array

Program


public class Main 
{
    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



More Questions


38 . Write a Java Program to find largest number in an array
39 . Write a Program to find second largest number in an array
40 . Write a Program to Find largest and smallest number in an array in java
41 . Write a Java Program to find largest and second largest in an array
42 . Write a java program to find the index of the largest number in an array
43 . Write a java program to Find the index of the smallest number in an array
44 . Write a Program to remove duplicate element in an array
45 . Write a java Program to print odd and even numbers from an array
46 . Write a java program to add two matrix
47 . Write a java Program to check given matrix is null matrix
48 . Add all the digits of a given positive integer until the result has a single digit



For latest job updates join Telegram Channel: https://t.me/sateeshm

Apply For The Job

Apply For Job

Java Material

Python Material

PLSQL Material

.Net Material

Java Practice Programs

400 Java Programs