Skip to content

Instantly share code, notes, and snippets.

@iJustErikk
Created December 15, 2020 14:34
Show Gist options
  • Save iJustErikk/c1faa631020398544d1e012277f219c4 to your computer and use it in GitHub Desktop.
Save iJustErikk/c1faa631020398544d1e012277f219c4 to your computer and use it in GitHub Desktop.
// checks two moving indices and stops when needed
const isPalindrome = (arr) => {
// symmetry will always be found in one or 0 elements
// [1] -> true
// [] -> true
if (arr.length < 2) return true;
// taking two pointer approach
let first = 0;
let last = arr.length - 1;
while (first < last) {
// if symmetry is broken, stop RIGHT there
if (arr[first] !== arr[last]) return false;
// walk our pointers
first += 1;
last -= 1;
}
// if symmetry is unbroken throughout string
// string is symmetrical
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment