w3resource

Java: Find the second smallest element in an array


18. Find second smallest array element

Write a Java program to find the second smallest element in an array.

Pictorial Presentation:

Java Array Exercises:  Find the second smallest element in an array


Sample Solution:

Java Code :

// Import the necessary Java utilities package.
import java.util.Arrays;

// Define a class named Exercise18.
public class Exercise18 {
    // The main method where the program execution starts.
    public static void main(String[] args) {
        // Create an integer array with numeric values.
        int[] my_array = {-1, 4, 0, 2, 7, -3};
        
        // Print the original numeric array.
        System.out.println("Original numeric array : " + Arrays.toString(my_array));
        
        // Initialize variables to find the minimum and second minimum values.
        int min = Integer.MAX_VALUE;
        int second_min = Integer.MAX_VALUE;
        
        // Iterate through the array to find the second lowest number.
        for (int i = 0; i < my_array.length; i++) {
            if (my_array[i] == min) {
                // If the current element equals the minimum, update the second minimum.
                second_min = min;
            } else if (my_array[i] < min) {
                // If the current element is less than the minimum, update both minimum and second minimum.
                second_min = min;
                min = my_array[i];
            } else if (my_array[i] < second_min) {
                // If the current element is less than the second minimum, update the second minimum.
                second_min = my_array[i];
            }
        }

        // Print the second lowest number found.
        System.out.println("Second lowest number is : " + second_min);
    }
}
 

Sample Output:

Original numeric array : [-1, 4, 0, 2, 7, -3]                                                                 
Second lowest number is : -1 

Flowchart:

Flowchart: Java exercises: Find the second smallest element in an array


For more Practice: Solve these Related Problems:

  • Write a Java program to find the smallest and second smallest elements in an array.
  • Write a Java program to find the second smallest element in an array using only one loop.
  • Write a Java program to find the second smallest element in an unsorted array without sorting.
  • Write a Java program to find the second smallest number when duplicates are present.

Go to:


PREV : Find second largest array element.
NEXT : Add two same-size matrices.

Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.