How to use Array Reversal In Javascript
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.
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);
Output
Not Palindrome
Time Complexity: O(n), where n is the number of elements in the array.
Space Complexity: O(n)
JavaScript Program to Check if an Array is Palindrome or Not
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.
Table of Content
- Using Array Reversal
- Using Iteration
- Using two pointer
- Using Array.every() Method: