
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Java program to find the largest number in an array
- Java program to find the 3rd largest number in an array
- Java program to find the 2nd largest number in an array
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Find the largest number in a Java array.
- Python Program to find the largest element in an array
- Golang Program to Find the Largest Element in an Array
- Find the 3rd largest number in a Java array.
- Find the 2nd largest number in a Java array.
- Python Program to find largest element in an array
- Program to find largest element in an array in C++
- Find the largest three elements in an array in C++
- Largest Palindrome Product in C++
- Find the largest pair sum in an unsorted array in C++
- Difference between the largest and the smallest primes in an array in Java
