Signup/Sign In

Java Program To Find the Second Largest and Smallest element in an Array

In this tutorial, we will learn how to find the second largest and second smallest elements in an array. The easiest way to find the two largest elements is by first sorting the elements and then returning the elements stored at the 1st and second last index of the array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the array elements: 7 6 9 2 4 1 3 6 9

Output: Second Smallest = 2

Second Largest = 7

Program 1: To Find the Second Largest and Second Smallest Element

In this approach, we will directly find the second largest and second smallest element in the array.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Use two for loops to display the second largest and second smallest element in an array.
  5. Use the first for loop to hold each element of the array
  6. Use the second for loop to compare the element with the rest of the elements.
  7. Swap the elements to sort the elements.
  8. Display the second largest and second smallest element.
  9. Stop

Below is the code for the same.

The below program demonstrates how to find the second largest and second smallest element in an array. Firstly, an array is declared and then initialized. With the help of two for loops, all the elements of the array are iterated and then the elements are compared and swapped in descending order. Then display the second largest and second smallest element respectively.

/*Java Program to find the second largest and second smallest elements in the array without using Functions*/
import java.util.Scanner;

public class FindElement
{

     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);
         int n;     //Declare array size
         System.out.println("Enter the size of the array");
         n=sc.nextInt();   //Initialize array size
         
         int arr[]=new int[n];   //Declare array 
        System.out.println("Enter the array");  
        for(int i=0;i<n;i++)     //Initialize array
        {
            arr[i]=sc.nextInt();
        }
               
        for(int i=0;i<n;i++)     //Use to hold the element
        {
            for(int j=i+1;j<n;j++)    //Use to compare with the rest of the elements 
            {
                if(arr[i]<arr[j])     //Check and swap
                {
                    int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        
        System.out.println("Second Largest element is "+arr[1]);   //Display second largest element.
        System.out.println("Second Smallest element is "+arr[n-2]);  //Display second smallest element.
    }
}


Enter the size of the array 10
Enter the array 56 7 6 45 3 4 23 12 21 1
Second Largest element is 45
Second Smallest element is 3

Program 2: To Find the Second Largest and Second Smallest Element

In this approach, we will use a separate method to find the second smallest and second-largest element in the array using Arrays.sort().

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Call a method that will display the second largest and second smallest elements in an array.
  5. Sort the array using Arrays.sort().
  6. Display the elements at the 1st and second last index.
  7. This is the second largest and second smallest element.
  8. Stop

Below is the code for the same.

The below program demonstrates how to use a separate method to find the second largest and second smallest element in an array using Arrays.sort(). Firstly, an array is declared and then initialized. A method is called next. The array is sorted using Arrays.sort() and the element at 1st and second last is displayed which is the second smallest and second largest element respectively.

/*Java Program to find the second largest and second smallest elements in the array using Functions*/
import java.util.Scanner;
import java.util.*;

public class findElement
{
    static void findLargest(int arr[], int n)      //Method to find the second largest and second smallest element
     {
        
       Arrays.sort(arr);   //Sorts the array in ascending order
        
        System.out.println("Second Largest element is "+arr[n-2]);  //Display Second Smallest
        System.out.println("Second Smallest element is "+arr[1]);  //Display Second Largest
         
     }
     
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);
         int n;     //Declare array size
         System.out.println("Enter the size of the array");
         n=sc.nextInt();   //Initialize array size
         
         int arr[]=new int[n];   //Declare array 
        System.out.println("Enter the array");  
        for(int i=0;i<n;i++)     //Initialize array
        {
            arr[i]=sc.nextInt();
        }
               
        findLargest(arr,n);  //Function Call
        
    }
}


Enter the size of the array 10
Enter the array 8 7 9 5 4 3 8 6 1 2
Second Largest element is 8
Second Smallest element is 2



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.