I've got an large array array1 filled with arrays of numbers already sorted like in the example below. Now I want to check, if the array1 contains the array2.
Currently I've got the function searchForArray which works fine. But as I've got very large arrays for array1, it's very slow.
How can I improve my search function for better performance?
var array1 = [
[1, 0], [1, 2], [1, 5], [1 , 12],
[2, 3], [2, 9], [2, 25],
[7, 2], [7, 4], [7, 7], [7, 8], [7, 16],
[8, 20], [8, 35], [8, 40], [8, 50]
];
var array2 = [7, 4];
if (searchForArray(array1, array2)) {
// Array 2 is in array1
}
function searchForArray(bigArray, searchArray) {
const a = JSON.stringify(bigArray);
const b = JSON.stringify(searchArray);
const c = a.indexOf(b);
if (c != -1) {
return true;
}
return false;
}
array1always have that sorted nature? the 1st element of the inner arrays seem to be sorted, you could always use that for a binary search.TreeMapandHashMap