I have been trying to figure out an efficient algorithm that can check if a list of values contains a list of values. Both lists are sorted in ascending order.
For example:
Check if
var listToSearch = [1,2,3,4,5,6,7]
contains
var searchValues = [1,2,3]
Without iterating through the list for each value in searchValues
. Nor do I want to use something like binary search for each value in searchValues
.
I tried creating a modified binary search for this but it does not work. Is there some modification of binary search but for multiple search values or any other algorithm designed for this problem?
Edit: I want to check if listToSearch
has ANY of the values in searchValues
(not the entire sublist). So, for example the algorithm could return [true, true, true] since 1,2, and 3 are all found in listToSearch