There are 2 arrays data1
and data2
. The desired result is an array containing all the elements in data1
which are larger than the largest element in data2
[+ the 3 before as defined in variable buffer
]. In other words, the result should also include the previous 3 elements in data1
preceding the matched elements. Both arrays are already sorted in ascending order.
My code below does what is required, but is there a better way to do the same thing? Will regex be faster?
Data set
data1 = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
data2 = [102, 104, 106, 108]
buffer = 3
The desired result is [106, 107, 108, 109, 110]
Algorithm
largestData2 = _.max(data2)
data1LargerThanData2 = _.filter(data1, function(data1) {
return data1 > largestData2
})
smallestData1LargerThanData2 = _.min(data1LargerThanData2)
sliceStart = data1.indexOf(smallestData1LargerThanData2) - buffer
result = data1.slice(sliceStart)