Find Second Smallest Element in the Array - The Coding Shala

Home >> Programming >> Find Second Smallest Element in the Array

 Hey there, welcome back to another post. In this post, we will learn how to find the second smallest number in the array in Java.

Find Second Smallest Element in the Array

Problem Statement

You have given an integer array, return the second smallest element from the array if exists else return no second smallest element exists.

Example 1: 

Input: 
[1, 4, 2, 7, 90, -1, -4]
Output:
-1

Find Second Smallest Element in the Array Solution using Single Iteration

Approach

By using the below steps we can find the second smallest element in the array by using a single iteration:

  • Initialize two variables first and second for smallest and second smallest numbers and the initial value will be Integer.MAX_VALUE.
  • Traverse the array and check if the current element is smaller than the smallest element then update both first and second.
  • If the current element is between first and second then only update the second.

Time Complexity: O(n)

Why? Because we are just using one for Loop.

Space Complexity: O(1)

Why? We are not using any extra space. Two variables first and second are constant.

Java Program: 

/**
 * https://www.thecodingshala.com/
 */

public class Main {

    public static int printSecondSmallest(int[] arr) {
        // size is less than 2
        if (arr.length < 2) {
            return Integer.MAX_VALUE;
        }

        int first = Integer.MAX_VALUE;
        int second = Integer.MAX_VALUE;

        for (int i = 0; i < arr.length; i++) {
            // if arr[i] is smaller than first and second
            // update both
            if (arr[i] < first) {
                second = first;
                first = arr[i];
            }

            // if arr[i] is between first and second then update second
            else if (arr[i] < second && arr[i] != first) {
                second = arr[i];
            }
        }

        return second;
    }

    public static void main(String[] args) {
        int[] arr = {3, 6, -1, 0, 55, 23, 56, 80, -96};
       // int[] arr = {1};
       // int[] arr = {1, 2, 2};
       // int[] arr = {1, 1, 1, 1};

        // print array
        System.out.println("The array is: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();

        int ele = printSecondSmallest(arr);
        if (ele == Integer.MAX_VALUE) {
            System.out.println("There is no second smallest element in the array");
        } else {
            System.out.println("The second smallest element is: " + ele);
        }
    }
}

Output: 

The array is: 
3 6 -1 0 55 23 56 80 -96 
The second smallest element is: -1


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala