beginnerArray ProblemsJava

Find the Smallest Element in an Array

Return the minimum value in an integer array using a single pass.

Quick Answer

Start by assuming the first element is the smallest. Walk the rest of the array once; whenever you find a value less than your current minimum, update the minimum. After a single pass the minimum holds the smallest element. Initialize from the first element, not a guessed large constant, so all inputs work.

Problem Statement

Given a non-empty array of integers, return its smallest element. You should do this in a single linear pass without sorting.

For example, in [4, 2, 8, 1, 5] the smallest element is 1. The array may contain negative numbers, so [-3, -7, -1] has smallest element -7.

Input: A non-empty array of integers arr.

Output: The smallest value contained in arr.

Examples

Example 1
Input:  [4, 2, 8, 1, 5]
Output: 1

1 is less than every other element in the array.

Example 2
Input:  [-3, -7, -1]
Output: -7

-7 is the most negative value and therefore the smallest.

Constraints

  • 1 <= arr.length
  • Elements may be negative, zero, or positive

Think Before You Code

Reveal the questions to ask yourself first
  • What should you initialize your running minimum to so large-valued arrays work?
  • How many passes over the array are needed?
  • What is a sensible response if the array were empty?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Do not initialize the minimum to 0 — an all-positive array would wrongly return 0.
Hint 2
Initialize the minimum to the first element, then compare against the rest.
Hint 3
Loop from index 1 to the end; whenever `arr[i] < min`, update `min = arr[i]`.

Approach

Reveal the step-by-step approach

Keep a running minimum and update it as you scan.

  1. Set min = arr[0] — a real element, so it is always valid.
  2. Loop i from 1 to arr.length - 1:
    • If arr[i] < min, set min = arr[i].
  3. Return min.

Seeding min with the first element (rather than 0 or Integer.MAX_VALUE) keeps the code correct for arrays of any magnitude and avoids a magic sentinel.

Dry Run

Walk through the example step by step

Finding the smallest in [4, 2, 8, 1, 5]:

start: min = arr[0] = 4

i | arr[i] | arr[i] < min? | min
--+--------+---------------+----
1 | 2      | yes           | 2
2 | 8      | no            | 2
3 | 1      | yes           | 1
4 | 5      | no            | 1
result: 1

Solution

Reveal the full Java solution
public class SmallestElement {
    public static int findSmallest(int[] arr) {
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        return min;
    }

    public static void main(String[] args) {
        System.out.println(findSmallest(new int[]{4, 2, 8, 1, 5})); // 1
        System.out.println(findSmallest(new int[]{-3, -7, -1}));    // -7
    }
}

One comparison per element gives a single linear pass, which is optimal — you must inspect every element to be certain of the minimum. Seeding from arr[0] makes all-positive and all-negative arrays work equally well without a sentinel.

Time: O(n) where n is the array lengthSpace: O(1)

Common Mistakes

  • Initializing min to 0, which returns 0 for an all-positive array.
  • Using > instead of < in the comparison, which finds the maximum by mistake.

Edge Cases to Test

  • A single-element array returns that element.
  • An all-positive array returns its smallest positive value, not 0.
  • An array with duplicate minimums returns that minimum value.

Interview Follow-Ups

  • How would you also return the index of the smallest element?
  • How would you find both the minimum and maximum in a single pass?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us