w3resource

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

Java Array: Exercise-41 with Solution

Write a Java program to find 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 java.util.*;
import java.lang.*;
public class Main
{
   public static void main (String[] args) 
    {
      int arr[] = {5, 7, -8, 5, 14, 1};
      
      int first_element, second_element, arr_size = arr.length;
 
        /* Return if the array size less than two */
        if (arr_size < 2)
        {
            System.out.println("Array size less than two.");
            return;
        }
 
        first_element = second_element = Integer.MAX_VALUE;
        for (int i = 0; i < arr_size ; i ++)
        {
            /* Update both first and second if 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];
        }
        if (second_element == Integer.MAX_VALUE)
            System.out.println("No second smallest element.");
        else
            System.out.println("The smallest element is " +
                               first_element + " and 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

Visualize Java code execution (Python Tutor):


Java Code Editor:

Improve this sample solution and post your code through Disqus

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?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH