I have two arrays, search
and target
. I want to find the longest sequence of elements of search
that starts from the beginning of search
and which also appears in the same consecutive order in target
. Then I want to return a copy of target
with those elements removed.
Here are some examples:
search = [4, "apple", 6, "turnip"]
target = [5, "apple", 4, "orange"]
=> [5, "apple", "orange"] # Delete [4], the longest matching
# prefix of `search`.
search = [4, "apple", 6, "turnip"]
target = [5, "apple", 4, "apple"]
=> [5, "apple"] # Delete [4, "apple"], the longest matching
# prefix of `search`.
search = [4, "apple", 6, "turnip"]
target = [5, "apple", 6, 7]
=> [5, "apple", 6, 7] # Nothing was matched; don't delete anything.
What's the most concise way to perform this check?