function findMax(nums) {
let max = nums[0];
for (let num of nums) {
if (num > max) max = num;
}
return max;
}
Explanation: We initialize the maximum value as the first element of the array. Then we iterate through the array and update the maximum whenever a larger element is found.
β± O(n) | πΎ O(1)