w3resource

Java: Find smallest and second smallest elements of a given array

Java Array: Exercise-41 with Solution

Write a Java program to find the smallest and second smallest elements of a given array.

Pictorial Presentation:

Java Array Exercises: Find smallest and second smallest elements of a given array

Sample Solution:

Java Code:

// Import necessary Java libraries.
import java.util.*;
import java.lang.*;

// Define a class named Main.
public class Main
{
    // The main method for executing the program.
    public static void main(String[] args) 
    {
        // Define an array of integers.
        int arr[] = {5, 7, -8, 5, 14, 1};
        
        int first_element, second_element, arr_size = arr.length;

        // Check if the array size is less than two.
        /* Return if the array size less than two */
        if (arr_size < 2)
        {
            System.out.println("Array size is less than two.");
            return;
        }

        // Initialize variables to find the first and second smallest elements.
        first_element = second_element = Integer.MAX_VALUE;
        
        // Loop through the array to find the smallest and second smallest elements.
        for (int i = 0; i < arr_size; i++)
        {
            /* Update both first and second if the current element is smaller than first. */
            if (arr[i] < first_element)
            {
                second_element = first_element;
                first_element = arr[i];
            }

            /* Update second if arr[i] is between first and second elements. */
            else if (arr[i] < second_element && arr[i] != first_element)
                second_element = arr[i];
        }
        
        // Check and print the results.
        if (second_element == Integer.MAX_VALUE)
            System.out.println("No second smallest element.");
        else
            System.out.println("The smallest element is " +
                               first_element + " and the second smallest element is " + second_element + ".");
    }
}

Sample Output:

                                                                              
The smallest element is -8 and second Smallest  element is 1.

Flowchart:

Flowchart: Find smallest and second smallest elements of a given array

Java Code Editor:

Previous: Write a Java program to find the two elements from a given array of positive and negative numbers such that their sum is closest to zero.
Next: Write a Java program to segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/array/java-array-exercise-41.php