BudiBadu Logo

Find Smallest

Array Easy 1 views
Like25

Ever had a list of prices and just needed to find the best deal? That’s what Find Smallest is all about! You’re given an array of integers, and your mission is to identify the single lowest numerical value hidden within that list. It requires careful checking to ensure you find the true absolute minimum.

The "secret sauce" here is a Linear Scan. You start with a very large number (or the first element in the array) and walk through the list, comparing each value as you go. Think of it like a "lowest price" guarantee: the current winner stays until something even smaller comes along. This approach is incredibly efficient, running in linear O(n) time, ensuring you find the base data point without any unnecessary complexity.

This is a fundamental skill for any data analysis task. Whether you''re finding the cheapest item or the lowest recorded temperature, mastering this minimum-finding logic is a basic building block for your coding career!

Examples

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 - Budibadu
Recommendation Algorithm Flow for Find Smallest - Budibadu

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;
    }
}