In the previous article, we have seen Java Program to Find the Array Type Even Odd and Mixed
In this article we are going to see how to find the longest palindrome in an Array using Java programming language, largest palindrome in string in java, program to find the largest palindrome in an array in c, string palindrome in java using array, length of longest palindrome in array, palindromic array.
Java Program to Find the Largest Palindrome in an Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to find the longest palindrome in an Array.
Method-1: Java Program to Find the Largest Palindrome In an Array of Strings
Approach:
- Initialize the array.
- Initialize two variables, int len = 0 (to store length of the largest palindrome) and String s = “” (store the largest palindrome).
- Iterate over the array.
- Check if the current element is a Palindrome.
- If yes check if its length is greater than len.
- If yes, store length of the current element in len and the element in s.
- After the loop is exhausted print the palindrome.
Program:
public class Main { public static void main(String[] args) { // initialize the array String[] s = { "10101", "984565489", "Radar", "Madam" }; //caling the findLongestPalindrome() method findLongestPalindrome(s); } //findLongestPalindrome() method to find longest palindrome static void findLongestPalindrome(String[] arr) { // initialize variables int len = 0; String longest = ""; for (String s : arr) { // check if the string is palindromic if (isPalindrome(s.toLowerCase())) { // update len and s if (s.length() > len) { len = s.length(); longest = s; } } } System.out.println("Longest palindrome in the array is \"" + longest + "\" of length " + len); } // method to check if the string is palindromic static boolean isPalindrome(String s) { StringBuffer sb = new StringBuffer(s); return s.equals(sb.reverse().toString()); } }
Output: Longest palindrome in the array is "984565489" of length 9
Method-2: Java Program to Find the Largest Palindrome In an Array of Integers
Approach:
- Create scanner class object.
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Sort the array.
- Iterate over the array from the end.
- Check if an element is palindrome, return the element.
Program:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num; System.out.print("Enter the number of elements in the array: "); num = sc.nextInt(); int arr[] = new int[num]; System.out.print("Enter the elements: "); for (int i = 0; i < num; i++) { arr[i] = sc.nextInt(); } //method called to find largest palindrome System.out.print("Largest Palindrome: " + find_largest_palindrome(arr)); } public static boolean is_palindrome(int n) { int div = 1; while (n / div >= 10) div *= 10; while (n != 0) { int first = n / div; int last = n % 10; // If first and last digits are not same then return false if (first != last) return false; // Removing the leading and trailing digits from the number n = (n % div) / 10; // Reducing divisor by a factor of 2 as 2 digits are dropped div = div / 100; } return true; } public static int find_largest_palindrome(int arr[]) { // sort the array Arrays.sort(arr); // check for the largest palindrome from the end for (int i = arr.length - 1; i >= 0; i--) { if (is_palindrome(arr[i])) return arr[i]; } return -1; } static boolean isPalindrome(String s) { StringBuffer sb = new StringBuffer(s); return s.equals(sb.reverse().toString()); } }
Output: Enter the number of elements in the array: 6 Enter the elements: 123123 456 121212 33 67676 9 Largest Palindrome: 67676
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Try Yourself:
- Write a program to find the largest palindrome in the string array in java?
- Write a program to find the largest palindrome in the string array?
- Write a program to find the largest palindrome in the string array in c?
- Write a program to find the largest palindrome in the string array in python?
- Find out whether the second largest number in an array is palindrome or not?
- Java program to find the largest number in an array?
- Java program to find the largest and smallest number in an array?
- Java program to find the largest element in an array?
- Program to find the largest palindrome in an array?
- Write a program to find the largest palindrome in the string array?
Related Java Programs: