Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Optimization of a set of exclusive filters

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

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda