To find the largest number in an array, iterate through the array and keep track of the largest number.
- Initialize a variable to store the largest number with a small value (e.g.,
-Infinity
). - Iterate through the array and compare each element to the current largest number.
- Update the largest number whenever a larger element is found.
- Return the largest number.
function findLargest(arr) {
let largest = -Infinity;
for (const num of arr) {
if (num > largest) {
largest = num;
}
}
return largest;
}
// Example usage
console.log(findLargest([5, 3, 8, 1, 6])); // Output: 8
console.log(findLargest([7, 4, 9, 2])); // Output: 9
This method has a time complexity of O(n), where n is the length of the array.
Tags: basic, JavaScript, Arrays, Algorithm