java

Find Second Smallest Element()

Parameters: int[] arr

The integer array to find the second smallest element in

Returns: Second smallest integer in the array

This function sorts through an array of integers, identifying and returning the second smallest number present within the array.

Arrays
Conditional Statements
Iteration
Variables
Return Statement
Medium dificulty

Writing a Java Method to Find the Second Smallest Element in an Array

Hello, fellow programmer! In the upcoming steps, you will uncover the way to create a function in Java that locally finds the second smallest element in an array. This isn't something overly complicated, yet it's extremely useful for enhancing your understanding of logical operations and array manipulation. Let's get coding!

Step 1: Define the Function and Initialize Variables

First, we will define a function called findSecondSmallest. Inside this function we will initialize two variables, min and secondMin, and set their values to Integer.MAX_VALUE. These two variables will be used to hold the minimum and second minimum values respectively from the array.

public static int findSecondSmallest(int[] arr) {
    int min = Integer.MAX_VALUE;
    int secondMin = Integer.MAX_VALUE;
}

Step 2: Loop Through the Array

We'll loop through the array using a for loop. For every element (arr[i]) in the array, if it's less than min, we'll update min and secondMin accordingly. If it's between min and secondMin, we update secondMin only.

for(int i=0; i < arr.length; i++) {
    if(arr[i] < min){
         secondMin = min;
         min = arr[i];
    } else if(arr[i] < secondMin && arr[i] != min){
        secondMin = arr[i];
    }
}

Step 3: Return Second Smallest Element

Finally, after looping through all the elements in the array, we return the value of secondMin, which holds the second smallest element in the array. If no such element exists, Integer.MAX_VALUE will be returned.

return secondMin;

Conclusion: Full Code Implementation

Here's the full implementation of the function findSecondSmallest:

public static int findSecondSmallest(int[] arr) {
    int min = Integer.MAX_VALUE;
    int secondMin = Integer.MAX_VALUE;
    for(int i=0; i < arr.length; i++) {
        if(arr[i] < min){
            secondMin = min;
            min = arr[i];
        } else if(arr[i] < secondMin && arr[i] != min){
            secondMin = arr[i];
        }
    }
    return secondMin;
}

The function works by continuously updating the values of min and secondMin as it iterates through the array, ensuring these variables always hold the smallest and second smallest values encountered so far.

Learn function in:

second smallest element

Finding the second smallest element in an array

Learn more

Mathematical principle

The principle used here is essentially an inspection of numerical order. If you had a list of numbers `1, 2, 3, 4, 5,` the second smallest would be `2`. The task is to automate this process programmatically without sorting the array first (which would have higher time complexity).

Learn more