Find Second Smallest Element in an Array in Java
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.
Java Program
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
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.
- 1
smallestandsecondSmallestboth start atInteger.MAX_VALUE, so even large array values update them correctly. - 2When a number beats
smallest, the oldsmallestvalue slides up intosecondSmallestbeforesmallestis updated. - 3When a number doesn't beat
smallestbut still beatssecondSmallest(and isn't a duplicate ofsmallest), onlysecondSmallestupdates. - 4After the full pass,
secondSmallestholds the answer.
[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.
Why: A single pass tracks two running values, so the extra memory stays constant regardless of array size.
Key Concepts
Approach 2: Sorting
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
Core Logic
Sorting a copy of the array puts every value in order, so the second smallest is simply the second element.
- 1
arr.clone()copies the array first, so sorting doesn't disturb the caller's original array. - 2
Arrays.sort(sorted)puts the copy into ascending order. - 3
sorted[1]reads the second element, which is the second smallest value.
[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.
Why: clone() copies every element into a new array, and sorting that copy costs O(n log n) comparisons.
Key Concepts
Approach 3: Java 8
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
Core Logic
Sorting the distinct values in ascending order and skipping the first one lands directly on the second smallest.
- 1
Arrays.stream(arr).boxed()converts theint[]into aStream<Integer>. - 2
.distinct()removes duplicate values, so a repeated smallest value doesn't occupy the runner-up spot. - 3
.sorted()puts the distinct values into their natural ascending order. - 4
.skip(1)drops the smallest value, and.findFirst()returns whatever comes next — the second smallest.
[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.
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.