Last active
October 30, 2020 14:59
-
-
Save jchiatt/88f02b068ae57f4ce8bba4023d81e808 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
Given two non-empty arrays of integers, write a function that determines whether the second array is a subsequence of the first one. | |
A subsequence of an array is a set of numbers that aren't necessarily adjacent in the array but that are in the same order as they appear in the array. | |
For instance, the numbers [1, 3, 4 form a subsequence of the array [1, 2, 3, 4] and so do the numbers [2, 4]. | |
Note that a single number in an array and the array itself are both valid subsequences of the array. | |
*Sample Input* | |
array = [5, 1, 22, 25, 6, -1, 8, 10]` | |
sequence = [1, 6, -1, 10] | |
*Sample Output* | |
true | |
*/ | |
function isValidSubsequence(array, sequence) { | |
let cursor = 0; | |
for (const v of array) { | |
if (cursor === sequence.length) break; | |
if (sequence[cursor] === v) cursor++; | |
} | |
return cursor === sequence.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment