Code Logo

Find Smallest

Published at10 Jan 2026
Easy 5 views
Like25

This problem asks for the smallest value in the array. You are not looking for a subarray or a special pattern. You just need the minimum element among all the numbers provided.

The answer is the value itself, not the position where it appears. Negative numbers matter here too. If an array includes negative values, the smallest number may be the most negative one.

For example, if nums = [10,5,8,3,12], the answer is 3. If nums = [0,-1,-5,2], the answer is -5. If the array has only one element, that element is both the smallest and the largest.

So the task is to inspect the array and return the minimum value it contains.

Example Input & Output

Example 1
Input
nums = [10, 5, 8, 3, 12]
Output
3
Explanation

Example 1: The smallest value in [10, 5, 8, 3, 12] is 3

Example 2
Input
nums = [0, -1, -5, 2]
Output
-5
Explanation

Example 2: The smallest value in [0, -1, -5, 2] is -5

Example 3
Input
nums = [7]
Output
7
Explanation

Example 3: With a single element, that element is the smallest

Algorithm Flow

Recommendation Algorithm Flow for Find Smallest
Recommendation Algorithm Flow for Find Smallest

Solution Approach

A single scan is enough here. We keep track of the smallest value seen so far and update it whenever we find something even smaller.

Start from the first element:

let minValue = nums[0];

Then compare the rest of the array against it:

for (let i = 1; i < nums.length; i++) {
  if (nums[i] < minValue) {
    minValue = nums[i];
  }
}

Every time a smaller number appears, we replace minValue. After the loop, it holds the minimum value in the whole array.

Then return it:

return minValue;

This solution runs in O(n) time and uses O(1) extra space.

Best Answers

java
class Solution {
    public int find_smallest(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int min = nums[0];
        for (int num : nums) {
            if (num < min) min = num;
        }
        return min;
    }
}