Hi recently I was asked a very interesting question: Giving a set of Data
data = [{name: "gem1", year: 2013, color:"yellow"}, {name: "gem2", year: 2013, color:"blue"}, {name: "gem3", year: 2021, color:"blue"}]
And a set of Filter
filters = [{k: "color", v: "yellow"}]
Show the data excluded by filter like:
getExclusive(data, filters) = [{name: "gem2", year: 2013, color:"blue"}, {name: "gem3", year: 2021, color:"blue"}]
Also the additional is that, question itself has mentioned that a very raw method:
filter.forEach(filter => {data = data.filter(datum => {return datum[filter.k] == filter.v})})
Is TOO SLOW. And the purpose is to optimize it.
Keep in mind that each object in data will have unknown number of keys, and the key names are also dynamic, for example: obj1 can only have {lastName: "last", firstName: "first"} and obj2 can only have {model: "Jeep", year: 2021, color: "white"})
I thought the reason why it's slow is because the big O goes to F * D.
And I want to convert the filters to a Map<string, Set> so that for every data it will look the location in O(1).
But then I found I was wrong. Because in this way it needs to go through every key in each obj to see if it hits a filter.
Therefore it becomes F + D * <avg key numbers in data>, which might be even worse if key number is so large?
So is there any other way to optimize it absolutely, or maybe this is just an open question with no definite answer?
Note that this question is in JavaScript. So I don't know if any internal APIs would be considered