Find Largest, Smallest, Second Largest and Second Smallest in an Array



In this problem we are given an array of numbers, and we have to find the largest, smallest, second largest, and second smallest elements in an array in Java.

Let's understand the problem better with the help of an example:

Input

int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12};

Output

Smallest element = 8
2nd Smallest element = 10
Largest element = 95
2nd Largest element = 90

To solve this problem, use the following approaches:

Using Arrays.sort()

In this approach, we sort the array using Arrays.sort() and get the largest, smallest, second largest, and second smallest elements directly using indexes.

Example

The following program uses arrays.sort() method to sort the array and get the required elements:

import java.util.*;
public class Demo {
   public static void main(String []args){
      int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12};
      System.out.println("Array = "+Arrays.toString(arr));
      Arrays.sort(arr);
      System.out.println("Smallest element = "+arr[0]);
      System.out.println("2nd Smallest element = "+arr[1]);
      System.out.println("Largest element = "+arr[arr.length - 1]);
      System.out.println("2nd Largest element = "+arr[arr.length - 2]);
   }
}

Output

The above program produces the following output :

Array = [55, 10, 8, 90, 43, 87, 95, 25, 50, 12]
Smallest element = 8
2nd Smallest element = 10
Largest element = 95
2nd Largest element = 90

Time Complexity: O(n log n)

Space Complexity: O(1)

Using Nested for loops

In this approach we use nested for loops to manually sort the array instead of using built-in methods like Arrays.sort(). After sorting the array, we get the required elements using their indexes.

Example

The following code shows how to implement this approach in java:

import java.util.*;
public class Demo {
   public static void main(String []args){
      int a;
      int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12};
      System.out.println("Array = "+Arrays.toString(arr));
      int count = arr.length;
      for (int i = 0; i < count; i++) {
         for (int j = i + 1; j < count; j++) {
            if (arr[i] > arr[j]) {
               a = arr[i];
               arr[i] = arr[j];
               arr[j] = a;
            }
         }
      }
      System.out.println("Smallest: "+arr[0]);
      System.out.println("Largest: "+arr[count-1]);
      System.out.println("Second Smallest: "+arr[1]);
      System.out.println("Second Largest: "+arr[count-2]);
   }
}

Output

The program produces the following output:

Array = [55, 10, 8, 90, 43, 87, 95, 25, 50, 12]
Smallest: 8
Largest: 95
Second Smallest: 10
Second Largest: 90

Time Complexity: O(n²)

Space Complexity: O(1)

Optimized approach

In this optimized approach, we find the required elements in a single pass without sorting the array. First, we iterate through the array using a for loop. During each iteration, we compare the element with maximum and minimum values and update them if required.

Example

The following code shows the implementation of this efficient approach:

import java.util.*;
public class Demo {
   public static void main(String []args){
      int arr[] = {55, 10, 8, 90, 43, 87, 95, 25, 50, 12};
      System.out.println("Array = "+Arrays.toString(arr));
      
      int max1 = Integer.MIN_VALUE;
      int max2 = Integer.MIN_VALUE;
      int min1 = Integer.MAX_VALUE;
      int min2 = Integer.MAX_VALUE;

      for (int i = 0; i < arr.length; i++) {
         if (arr[i] > max1) {
            max2 = max1;
            max1 = arr[i];
         } else if (arr[i] > max2) {
            max2 = arr[i];
         }

         if (arr[i] < min1) {
            min2 = min1;
            min1 = arr[i];
         } else if (arr[i] < min2) {
            min2 = arr[i];
         }
      }

      System.out.println("Smallest element = "+min1);
      System.out.println("2nd Smallest element = "+min2);
      System.out.println("Largest element = "+max1);
      System.out.println("2nd Largest element = "+max2);
   }
}

Output

The above program produced the following output :

Array = [55, 10, 8, 90, 43, 87, 95, 25, 50, 12]
Smallest element = 8
2nd Smallest element = 10
Largest element = 95
2nd Largest element = 90

Time Complexity: O(n)

Space Complexity: O(1)

Updated on: 2025-04-16T15:53:47+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements