JS Built-ins
← Array161 / 226
ArrayArray.prototype.reverseadvanced

Array.prototype.reverse() β€” check if array is a palindrome

πŸ“– Usage Example
const arr = [1, 2, 3]
arr.join('') === [...arr].reverse().join('')
// β†’ false (not a palindrome)

Compare an array with its reverse to check for palindromes without mutating the original.

Palindrome check

An array is a palindrome if it reads the same forwards and backwards: [1, 2, 1] is a palindrome, [1, 2, 3] is not.

Compare each element with its mirror index to check efficiently.

arr.every((val, i) => val === arr[arr.length - 1 - i])
πŸ’‘ Hints (2)
  • β€’ Use `arr.every((val, i) => val === arr[arr.length - 1 - i])` to compare each element with its mirror.
  • β€’ The mirror index of `i` in an array of length `n` is `n - 1 - i`.