Skip to content

Instantly share code, notes, and snippets.

@ilyas-shah
Created September 6, 2022 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyas-shah/d990852344f2ed0268fdf54424b96012 to your computer and use it in GitHub Desktop.
Save ilyas-shah/d990852344f2ed0268fdf54424b96012 to your computer and use it in GitHub Desktop.
Find sub-sequence in an array - Javascript
function findSubsequence (arr = [], subArr = []) {
let prevIndex = 0;
for(let i = 0; i < subArr.length; i++) {
const item = subArr[i];
const index = arr.indexOf(item);
if ( index >= 0) {
if (prevIndex > index) return false;
prevIndex = index;
}
return false;
}
return true;
}
console.log(findSubsequence([1,6,7,0,1,9,10,-1], [1,10,6]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment