Say I have two arrays, items and removeItems and I wanted any values found in removeItems to be removed from items.
The brute force mechanism would probably be:
var animals = ["cow","dog","frog","cat","whale","salmon","zebra","tuna"];
var nonMammals = ["salmon","frog","tuna","spider"];
var mammals = [];
var isMammal;
for(var i=0;i<animals.length;i++){
isMammal = true;
for(var j=0;j<nonMammals;j++){
if(nonMammals[j] === animals[i]){
isMammal = false;
break;
}
}
if(isMammal){
mammals.push(animals[i]);
}
}
This is what? O(N^2)? Is there a more efficient way?
That’s actually
O(M * N).Probably you could do better sorting the
animalsarray first, then doing a binary search. You’ll be able to reduce toO(N * log N)– well, that’s iflog N < Manyway.Anyway, if you’re working with JS and that runs client-side, then just try to keep amount of data at minimum, or their browsers will yell at you with every request.