What is Palindrome array?
Palindrome means same when read from both ends.
This term may be applied to String, numbers and arrays.
When applied to String and numbers, it means the original String and number are the same when reversed. Examples are “wow”, 13631 etc.
When applied to arrays, the word Palindrome means the array contents remain same when array is reversed, i.e., when last element is made the first element, second last element is switched with second element and so on. Example of a Palindrome array is [12, 32, 67, 32, 12].
This array when reversed becomes [12, 32, 67, 32, 12] which is identical to the original array.

Method 1 : Using loop
Loop over the array and compare the element from left end of the array with the corresponding element from the right end of the array.
At any point, if the elements do not match, terminate the loop. If the whole loop completed, then the array is palindrome.

public boolean isPalindrome(int[] array) {
   int length = array.length;
   for (int index = 0; index < array.length; index++) {
	// get the element from the start
	int start = array[index];
	// get corresponding element from end
	int end = array[--length];
	// check if elements till the middle have been compared
	if (length &lt; index) {
		return true;
	// if start element is not the same as end element, the array is not
	// palindrome
	if (start != end) {
		return false;
   // if the control reaches here, means all the elements were same 
   return true;

Method 2 :Using Recursion
This approach also works same as the above but difference is that it does not use any loop to traverse the array. Instead it uses recursion, which means the method calls itself till all the elements are compared.
This method also compares element at the left end of the array with the corresponding element at the right end.
It accepts the array to be checked, left(or start) index and right(or end) index as arguments.
If elements at both index are same, then it calls itself with start index incremented and end index decreased by 1 till start index becomes greater than the end index.
If the elements at any iteration are not equal, then the method returns and recursion is terminated.

public boolean isPalindrome(int[] array, int startIndex, int endIndex) {
    // if array is empty or has 1 element, it is palindrome
    if (array.length == 0 || array.length == 1)
	return true;
    // check if start index is greater than end index, which means
    // whole array has been checked
    if (startIndex >= endIndex)
	return true;
    // check if element from left is not equal to element from last, 
    // then array is not palindrome
    if (array[startIndex] != array[endIndex])
	return false;
    // check for palindrome with start index incremented and end index
    // decreased by 1
    return isPalindrome(array, startIndex + 1, endIndex - 1);

That’s it for this post. If you are familiar with any other method, then do let us know by commenting in the space below.

Hit the clap if the article was useful.

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.