JavaScript Program to Check if an Array is Palindrome or Not
Last Updated :
06 Jun, 2024
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. To check if an array is a palindrome, compare it to its reverse version. If they match, it's a palindrome.
Given an array, the task is to determine whether an array is a palindrome.
Examples:
Input: arr = [3, 6, 0, 6, 3]
Output: Palindrome
Input: arr = [1, 2, 3, 4, 5]
Output: Not Palindrome
Below are the approaches to checking if an array is palindrome or not.
Using Array Reversal
In this approach, we are using array reversal to create a reversed copy of the original array, then comparing both arrays' string representations to determine if the array is a palindrome or not.
Example: The below example uses Array Reversal to check if an array is palindrome or not.
JavaScript
let arr = [1, 2, 3, 4, 5, 6];
let reversedArr = [...arr].reverse();
let res = JSON.stringify(arr) ===
JSON.stringify(reversedArr) ? "Palindrome" : "Not Palindrome";
console.log(res);
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n)
Using Iteration
In this approach, we are using iteration with two pointers starting from both ends towards the middle of the array, comparing corresponding elements to determine if the array is a palindrome or not.
Example: The below example uses Iteration to check if an array is palindrome or not.
JavaScript
let arr = [3, 6, 0, 6, 3];
let res = "Palindrome";
for (let i = 0; i < arr.length / 2; i++) {
if (arr[i] !== arr[arr.length - 1 - i])
{
res = "Not Palindrome";
break;
}
}
console.log(res);
Time Complexity: O(n/2) = O(n), where n is the number of elements in the array.
Space Complexity: O(1)
Using two pointer
In this approach we use two pointers, one at the start and one at the end of the array. then we use a while loop to iterate until the pointers meet. We compare elements at the pointers and move them towards each other. If at any point, the elements are not equal means array is not palindrome.
Example: The below example uses two pointer approach to check if an array is palindrome or not.
JavaScript
function isPalindrome(arr) {
let start = 0;
let end = arr.length - 1;
while (start < end) {
if (arr[start] !== arr[end]) {
return 'Not palindrome';
}
start++;
end--;
}
return 'Palindrome';
}
const array1 = [1, 2, 3, 2, 1];
console.log(isPalindrome(array1));
Using Array.every() Method:
In this approach, we utilize the Array.every() method to check if the array is a palindrome. We compare each element of the array with its corresponding element from the reverse side of the array. If all the elements match, the array is a palindrome; otherwise, it's not.
Example:
JavaScript
function isPalindrome(arr) {
return arr.every((element, index) => element === arr[arr.length - 1 - index]);
}
const array1 = [1, 2, 3, 2, 1];
const array2 = [1, 2, 3, 4, 5];
console.log(isPalindrome(array1) ? 'Palindrome' : 'Not Palindrome');
console.log(isPalindrome(array2) ? 'Palindrome' : 'Not Palindrome');
OutputPalindrome
Not Palindrome
Similar Reads
JavaScript Program to Check for Palindrome Number We are going to learn about Palindrome Numbers in JavaScript. A palindrome number is a numerical sequence that reads the same forwards and backward, It remains unchanged even when reversed, retaining its original identity. Example: Input : Number = 121Output : PalindromeInput : Number = 1331Output :
4 min read
JavaScript Program to Check if a Given String is a Rotation of a Palindrome In this article, we will see how to check a given string is a rotation of a palindrome. A palindrome is a string that remains the same when its characters are reversed (e.g., "racecar" is a palindrome).Example:Input: str = "racecar"Output: true // "racecar" is a rotation of a palindrome "racecar"Inp
6 min read
JavaScript Program to Check for Palindrome String using Recursion Given a string, write a recursive function that checks if the given string is a palindrome, else, not a palindrome. A string is called a palindrome if the reverse of the string is the same as the original one. For example - âmadamâ, âracecarâ, etc.What is Recursion?The process in which a function ca
3 min read
Javascript Program For Checking Linked List With A Loop Is Palindrome Or Not Given a linked list with a loop, the task is to find whether it is palindrome or not. You are not allowed to remove the loop. Examples: Input: 1 -> 2 -> 3 -> 2 /| |/ ------- 1 Output: PalindromeLinked list is 1 2 3 2 1 which is a palindrome.Input: 1 -> 2 -> 3 -> 4 /| |/ ------- 1 O
4 min read
How to check whether a passed string is palindrome or not in JavaScript? We are given a string, our task is to find string is palindrome or not. A palindrome is a series of numbers, strings, or letters that, when read from right to left and left to right, match each other exactly or produce the same series of characters. in simple words when number strings or characters
4 min read
R Program to Check if a String is a Palindrome In this article, we explore a simple yet essential task in programming: checking whether a given string is a palindrome. A palindrome is a sequence of characters that reads the same forwards and backwards, making it a common problem in text processing and string manipulation. We'll delve into the lo
5 min read