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
- Start
- Declare an array.
- Initialize the array.
- Use two for loops to display the second largest and second smallest element in an array.
- Use the first for loop to hold each element of the array
- Use the second for loop to compare the element with the rest of the elements.
- Swap the elements to sort the elements.
- Display the second largest and second smallest element.
- 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
- Start
- Declare an array.
- Initialize the array.
- Call a method that will display the second largest and second smallest elements in an array.
- Sort the array using
Arrays.sort()
.
- Display the elements at the 1st and second last index.
- This is the second largest and second smallest element.
- 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