# How to Find the Largest Palindrome in an Array in Java?

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to find the largest palindrome in an array. A number is said to be a palindrome number if after reversing the same number it is equal to the original number.

Let’s explore the article to see how it can be done by using Java programming language.

### To Show You Some Instances

#### Instance-1

Suppose the original array is {857, 232, 1996991, 54545}


After finding the largest palindrome of an array the result will be −

1996991 is the largest palindrome of a given array.


#### Instance-2

Suppose the original array is {2357, 23232, 568568, 1238321}


After finding the largest palindrome of an array the result will be −

1238321 is the largest palindrome of a given array.


#### Instance-3

Suppose the original array is {557, 2325532, 56465, 6238326}


After finding the largest palindrome of an array the result will be −

6238326 is the largest palindrome of a given array.


### Algorithm

• Step 1 − Declare and initialize an integer array.

• Step 2 − Sort the array in ascending order.

• Step 3 − Get the largest element in an array.

• Step 4 − Reverse the largest element.

• Step 5 − check for palindrome.

• Step 6 − Print the result.

### Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length


Where ‘array’ refers to the array reference.

### Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of Array.

• By Using User Defined Method.

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Array

### Example

In this approach, array elements will be initialized in the program. Then as per the algorithm find the largest palindrome in an array.

import java.util.*;
public class Main {

public static void main(String args[]){

//Declare and initialize the array elements
Integer arr[] = { 857, 232, 1996991, 54545 };
System.out.println("Given array is: "+Arrays.toString(arr));

//sorting array in ascending order
Arrays.sort(arr);
for (int i = arr.length - 1; i >= 0; i--){
String reverse = "";

//getting the largest element in an array
String original = Integer.toString(arr[i]);

//reversing the largest element
int length = original.length();
for ( int j = length - 1; j >= 0; j-- )  {
reverse = reverse + original.charAt(j);
}

//checking for palindrome
if (original.equals(reverse))  {
System.out.println(arr[i] + " is the largest palindrome of a given array.");
}
break;
}
}
}


### Output

Given array is: [857, 232, 1996991, 54545]
1996991 is the largest palindrome of a given array.


## Approach-2: By Using User Defined Method

### Example

In this approach, array elements will be initialized in the program. Then call a user-defined method by passing the array as a parameter and inside the method as per the algorithm find the largest palindrome in an array.

import java.util.*;
public class Main {
public static void main(String args[]){

//Declare and initialize the array elements
Integer arr[] = { 557, 2325532, 56465, 6238326 };
System.out.println("Given array is: "+Arrays.toString(arr));

//calling user defined method
func(arr);
}

//user defined method
static void func(Integer arr[]){

//sorting array in ascending order
Arrays.sort(arr);
for (int i = arr.length - 1; i >= 0; i--){
String reverse = "";

//getting the largest element in an array
String original = Integer.toString(arr[i]);

//reversing the largest element
int length = original.length();
for ( int j = length - 1; j >= 0; j-- )  {
reverse = reverse + original.charAt(j);
}

//checking for palindrome
if (original.equals(reverse))  {
System.out.println(arr[i] + " is the largest palindrome of a given array.");
}
break;
}
}
}


### Output

Given array is: [557, 2325532, 56465, 6238326]
6238326 is the largest palindrome of a given array.


In this article, we explored different approaches to find the largest palindrome in an array by using Java programming language.