Find Second Smallest Element in an Array

Understanding Second Smallest Element

Finding the second smallest element in an array involves identifying the smallest element and then searching for the next smallest value.

We will explore three different methods to find the second smallest element in an array using Java.

Method 1: Using Iteration

This method iterates through the array to find the smallest and second smallest elements.

import java.util.*;

public class SecondSmallest {
    public static void findSecondSmallest(int[] arr) {
        int smallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE;
        for (int num : arr) {
            if (num < smallest) {
                secondSmallest = smallest;
                smallest = num;
            } else if (num < secondSmallest && num != smallest) {
                secondSmallest = num;
            }
        }
        System.out.println("Second Smallest Element: " + secondSmallest);
    }

    public static void main(String[] args) {
        int[] arr = {10, 20, 4, 45, 99, 23};
        findSecondSmallest(arr);
    }
}
            
Output: Second Smallest Element: 10

Method 2: Using Sorting

This method sorts the array and takes the second element as the second smallest.

import java.util.*;

public class SecondSmallest {
    public static void findSecondSmallest(int[] arr) {
        Arrays.sort(arr);
        System.out.println("Second Smallest Element: " + arr[1]);
    }

    public static void main(String[] args) {
        int[] arr = {10, 20, 4, 45, 99, 23};
        findSecondSmallest(arr);
    }
}
            
Output: Second Smallest Element: 10

Method 3: Using Recursion

This method finds the second smallest element using recursion.

import java.util.*;

public class SecondSmallest {
    public static void findSecondSmallestRecursive(int[] arr, int n, int[] smallest, int[] secondSmallest) {
        if (n == 0) return;
        if (arr[n - 1] < smallest[0]) {
            secondSmallest[0] = smallest[0];
            smallest[0] = arr[n - 1];
        } else if (arr[n - 1] < secondSmallest[0] && arr[n - 1] != smallest[0]) {
            secondSmallest[0] = arr[n - 1];
        }
        findSecondSmallestRecursive(arr, n - 1, smallest, secondSmallest);
    }

    public static void main(String[] args) {
        int[] arr = {10, 20, 4, 45, 99, 23};
        int[] smallest = {Integer.MAX_VALUE}, secondSmallest = {Integer.MAX_VALUE};
        findSecondSmallestRecursive(arr, arr.length, smallest, secondSmallest);
        System.out.println("Second Smallest Element: " + secondSmallest[0]);
    }
}
            
Output: Second Smallest Element: 10
Top 100 Codes By Learn-for-free
Start Preparing Arraysform here👇

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Find Largest Element in an Array: C C++ Java Python

Find Smallest Element in an Array: C C++ Java Python

Find the Smallest and Largest Element in an Array: C C++ Java Python

Find Second Smallest Element in an Array: C C++ Java Python

Calculate the Sum of Elements in an Array: C C++ Java Python

Reverse an Array: C C++ Java Python

Sort First Half in Ascending Order and Second Half in Descending: C C++ Java Python

Finding the Frequency of Elements in an Array: C C++ Java Python

Counting the Number of Even and Odd Elements in an Array: C C++ Java Python

Finding Maximum Product Sub-array in a Given Array: C C++ Java Python

Finding Arrays are Disjoint or Not: C C++ Java Python

Finding Equilibrium Index of an Array: C C++ Java Python

Rotation of Elements of Array - Left and Right: C C++ Java Python

Balanced Parenthesis Problem: C C++ Java Python