BudiBadu Logo

Find Max Number

Array Easy 24 views
Like11

Ever had a list of data and just needed to find the "peak" value? That’s exactly what Find Max Number is all about! You’re given an array of integers, and your mission is to identify the single largest numerical value hidden within that list. It sounds simple, but you’ll need to handle negatives, duplicates, and arrays of all sizes.

The "secret sauce" here is a Linear Scan. You start with a very small number (or the first element in the array) and walk through the list, comparing each value as you go. Think of it like a king-of-the-hill game: the current candidate stays on top until a bigger number comes along to take its place. This approach is incredibly efficient, running in O(n) time, ensuring you find the true peak data point within the provided array.

This is a fundamental skill for any data analysis task. Whether you''re finding the top score or the highest temperature, mastering this peak-finding logic is a basic building block for any developer.

Examples

Example 1
Input
nums = [1, 5, 3, 9, 2]
Output
9
Explanation

Example 1: The maximum value in [1, 5, 3, 9, 2] is 9

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

Example 2: The maximum value in [-10, -5, -20, -1] is -1

Example 3
Input
nums = [42]
Output
42
Explanation

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

Algorithm Flow

Recommendation Algorithm Flow for Find Max Number - Budibadu
Recommendation Algorithm Flow for Find Max Number - Budibadu

Best Answers

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