# Find the Longest Palindrome in an Array using Python

## Longest Palindrome in an Array

Here, in this page we will discuss the program to find the longest palindrome in an array in python programming language, we will discuss two different methods to find the longest palindromic element in the given array.

Here in this page we will discuss the following methods to find the longest palindromic number.

• Method 1 : Using Naive Approach
• Method 2 : Using Sorting

## Method 1 :

• Create a function ispalindrome(int n), it will return 1 if the passing number is palindrome otherwise return 0.
• Inside the main function, create a variable say res = -1, that hold the maximum palindrome number.
• Run a loop from [0, n),  and check if(ispalindrome(arr[i]) && res<arr[i]), then set res = arr[i].
• Print the value of res.

### Method 1 : Code in Python

Run
```# Function to check if n is palindrome
def isPalindrome(n):

divisor = 1
while (int(n / divisor) >= 10):
divisor *= 10

while (n != 0):
trailing = n % 10

return False

n = int((n % divisor) / 10)

divisor = int(divisor / 100)
return True

# Function to find the largest palindromic element
def largestPalindrome(arr, n):
currentMax = -1

for i in range(0, n, 1):
if (arr[i] > currentMax and isPalindrome(arr[i])):
currentMax = arr[i]

return currentMax

# Driver Code

arr = [1, 232, 5545455, 909090, 161]
n = len(arr)

print(largestPalindrome(arr, n))```

### Output

`5545455`

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Method 2 :

In this method we first sort the array and then start traversing the array from end, and return the element that satisfy the condition, that it is palindrome.

• Sort the array using inbuilt sort() function.
• Start traversing from end of the array,
• Print the element if it is palindrome.
• If no element found then print -1.

### Method 2 : Code in Python

Run
```# Function to check if n is palindrome
def isPalindrome(n):

divisor = 1
while (int(n / divisor) >= 10):
divisor *= 10

while (n != 0):
trailing = n % 10

return False

n = int((n % divisor) / 10)

divisor = int(divisor / 100)
return True

# Function to find the largest palindromic element
def largestPalindrome(A, n) :
# Sort the array
A.sort()

for i in range(n - 1, -1, -1) :
# If number is palindrome
if (isPalindrome(A[i])) :
return A[i]

# If no palindromic number found
return -1

# Driver Code
arr = [1, 232, 5545455, 909090, 161]
n = len(arr)
print(largestPalindrome(arr, n))```

### Output

`5545455`

### 20 comments on “Find the Longest Palindrome in an Array using Python”

• goldberg.joeeee

#return the longest palindrome from the array

class CheckLargestPalindrome:

def check_palindrome(self, string):
return string == string[::-1]

def get_longest_palindrome(self, arr):
longest_pal = ”
for i in range(len(arr)):
if self.check_palindrome(str(arr[i])):
if len(longest_pal) < len(str(arr[i])):
longest_pal = str(arr[i])

return len(longest_pal)

arr = [1, 232, 5545455, 909090, 161]
p1 = CheckLargestPalindrome()
#print(p1.get_longest_palindrome(arr))
# Generate test cases
test_cases = [
[1, 232, 5545455, 909090, 161], # Example case with multiple palindromes
[123, 456, 789], # No palindromes in the array
[111, 222, 333, 444, 555], # All elements are palindromes
[12, 34, 56, 78, 90], # No palindromes in the array
[121, 232, '', 454, 565],
[1, 212, '', 123, 32123] # All elements are palindromes
]

# Test the get_longest_palindrome function with the test cases
for arr in test_cases:
p1 = CheckLargestPalindrome()
longest_palindrome = p1.get_longest_palindrome(arr)
print(f"Longest palindrome in {arr}: {longest_palindrome}")

• Arjun

arr=[1,121,232,5545455, 909090,111111]
length=dict()
for i in arr:
length[i]=len(str(i))
print(length)

def ispalindrome(n):
r=str(n)[::-1]
if str(n)==r:
return True
return False
maxlength=-1
for i in arr:
if ispalindrome(i):
if length[i]>maxlength:
maxlength=length[i]
# print(maxlength)
for key, value in length.items():
if value==maxlength:
print(key)

• Arjun

for key, value in length.items():
if value==maxlength and ispalindrome(key):
print(key)

• Narendra Patil

arr = [1, 232, 5545455, 909090, 161,245,15526]
ans_arr = []
for i in arr:
temp = i
reverse=0
while temp>0:
rem = temp%10
reverse = reverse*10+rem
temp = temp//10
if(i==reverse):
ans_arr.append(i)
print(max(ans_arr))

• vignesh

arr = [1, 232, 5545455, 909090, 161,123]
new_arr=[]
for i in range(len(arr)):
string=str(arr[i])
if string==string[::-1]:
new_arr.append(int(string))
print(max(new_arr))

• DuttLuri Lalithasri

lst = [1, 232, 5545455, 909090, 161]
out = []
for i in lst:
if str(i)==str(i)[::-1]:
out+=[i]
s = max(out)
print(‘The longest palindrome of an array is :’,s)

• DS-30

arr = [1, 232, 5545455, 909090, 161]
result = []
for i in arr:
temp = str(i)
a = str(i)
a = a[::-1]
if temp == a:
result.append(int(a))
print(max(result))