To find the largest palindrome in an array, iterate through the array and check if each number is a palindrome, keeping track of the largest one.
- Iterate through the array and check if each element is a palindrome.
- For each palindrome, compare it with the current largest palindrome.
- Return the largest palindrome found.
function isPalindrome(num) {
return num.toString() === num.toString().split('').reverse().join('');
}
function findLargestPalindrome(arr) {
let largest = -Infinity;
for (let num of arr) {
if (isPalindrome(num) && num > largest) {
largest = num;
}
}
return largest === -Infinity ? 'No palindrome found' : largest;
}
// Example usage
console.log(findLargestPalindrome([121, 123, 232, 456])); // Output: 232
console.log(findLargestPalindrome([123, 456, 789])); // Output: No palindrome found
This method has a time complexity of O(n), where n is the length of the array.
Tags: intermediate, JavaScript, Arrays, Algorithm