To find the largest even number in an array, iterate through the array and check each element for evenness, keeping track of the largest even number.
- Initialize a variable to store the largest even number, starting with a value like
-Infinity
. - Iterate through the array and check if each element is even.
- If the element is even and larger than the current largest even number, update the largest even number.
- Return the largest even number.
function findLargestEven(arr) {
let largestEven = -Infinity;
for (const num of arr) {
if (num % 2 === 0 && num > largestEven) {
largestEven = num;
}
}
return largestEven === -Infinity ? null : largestEven;
}
// Example usage
console.log(findLargestEven([1, 2, 3, 4, 5])); // Output: 4
console.log(findLargestEven([7, 5, 9, 1])); // Output: null
This method has a time complexity of O(n), where n is the length of the array.
Tags: intermediate, JavaScript, Arrays, Algorithm