Java ProgramsArraysFind Second Smallest Element in an Array

Find Second Smallest Element in an Array in Java

beginner·  Arrays  ·  Array

Problem

The second smallest element is the smallest value in the array after excluding the single smallest one.

Given an array of integers, find its second smallest element.

Input
[12, 45, 3, 68, 22, 7]
Output
Second smallest: 7

Java Program

Java
public class SecondSmallest { public static void main(String[] args) { int[] arr = {12, 45, 3, 68, 22, 7}; int smallest = Integer.MAX_VALUE, secondSmallest = Integer.MAX_VALUE; for (int num : arr) { if (num < smallest) { secondSmallest = smallest; // old smallest slides up smallest = num; } else if (num < secondSmallest && num != smallest) { secondSmallest = num; } } System.out.println("Second smallest: " + secondSmallest); } }

Output

Second smallest: 7

Core Logic

Tracking both the smallest and second-smallest value at once, updating both together whenever a new smallest is found, gets the answer in a single pass.

How It Works
  1. 1smallest and secondSmallest both start at Integer.MAX_VALUE, so even large array values update them correctly.
  2. 2When a number beats smallest, the old smallest value slides up into secondSmallest before smallest is updated.
  3. 3When a number doesn't beat smallest but still beats secondSmallest (and isn't a duplicate of smallest), only secondSmallest updates.
  4. 4After the full pass, secondSmallest holds the answer.
For [12, 45, 3, 68, 22, 7], smallest/secondSmallest update to 12/MAX, then 3/12, then 3/7 once 7 is scanned — leaving secondSmallest at 7.
💡

Key Point: The num != smallest check matters — without it, a duplicate of the smallest value would incorrectly overwrite secondSmallest with a copy of the smallest instead of the true runner-up.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: A single pass tracks two running values, so the extra memory stays constant regardless of array size.

Key Concepts

for-each looprunning two minimums

Approach 2: Sorting

Java
import java.util.Arrays; public class SecondSmallestSorted { public static void main(String[] args) { int[] arr = {12, 45, 3, 68, 22, 7}; int[] sorted = arr.clone(); // sort a copy, not the caller's original array Arrays.sort(sorted); System.out.println("Second smallest: " + sorted[1]); // second element after sorting } }

Output

Second smallest: 7

Core Logic

Sorting a copy of the array puts every value in order, so the second smallest is simply the second element.

How It Works
  1. 1arr.clone() copies the array first, so sorting doesn't disturb the caller's original array.
  2. 2Arrays.sort(sorted) puts the copy into ascending order.
  3. 3sorted[1] reads the second element, which is the second smallest value.
Sorting [12, 45, 3, 68, 22, 7] gives [3, 7, 12, 22, 45, 68], and the second element is 7.
💡

Key Point: This assumes the smallest value appears only once — with duplicates at the bottom, the second element after sorting could just be another copy of the smallest, unlike the single-pass version's explicit duplicate check.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

Why: clone() copies every element into a new array, and sorting that copy costs O(n log n) comparisons.

Key Concepts

Arrays.sort()array cloning

Approach 3: Java 8

Java
import java.util.Arrays; public class SecondSmallestStream { public static void main(String[] args) { int[] arr = {12, 45, 3, 68, 22, 7}; // Sorts the distinct values ascending and skips the smallest int secondSmallest = Arrays.stream(arr) .boxed() .distinct() .sorted() .skip(1) .findFirst() .orElseThrow(); System.out.println("Second smallest: " + secondSmallest); } }

Output

Second smallest: 7

Core Logic

Sorting the distinct values in ascending order and skipping the first one lands directly on the second smallest.

How It Works
  1. 1Arrays.stream(arr).boxed() converts the int[] into a Stream<Integer>.
  2. 2.distinct() removes duplicate values, so a repeated smallest value doesn't occupy the runner-up spot.
  3. 3.sorted() puts the distinct values into their natural ascending order.
  4. 4.skip(1) drops the smallest value, and .findFirst() returns whatever comes next — the second smallest.
For [12, 45, 3, 68, 22, 7], sorting the distinct values ascending gives [3, 7, 12, 22, 45, 68]; skipping the first leaves 7 as the answer.
💡

Key Point: .distinct() here does the same job as the single-pass version's num != smallest check — both exist to stop a duplicate of the smallest value from being reported as the runner-up.

Complexity
Time Complexity: O(n log n)Space Complexity: O(n)

Why: distinct() and sorted() together still perform a comparison sort over the boxed values, and boxing to Integer plus the sort's internal buffer both cost O(n) space.

Key Concepts

Streamdistinct()sorted()skip()

Related Programs