Check whether a given array has a palindrome in Javascript.
Below logic should be fast, if not the fastest.
|
|
The code is simple -
- If given array length is even, it can’t be a palindrome
- Go through half the array, and find out whether array element is equal to the corresponding element on the “other side”.
In other words, check whether first element is equal to last element, the second element is equal to the last but one element, and so on. - Repeat (2) until you hit the end (in which case given array is palindrome), or hit an element pair that is not equal (not a palindrome)