Created
September 6, 2022 18:39
-
-
Save ilyas-shah/d990852344f2ed0268fdf54424b96012 to your computer and use it in GitHub Desktop.
Find sub-sequence in an array - Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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