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

169
Visualizações
Time complexity of traversing nested JS object

I would like to know in which time complexity I am traversing my nested JS Object. For traversing the JS Object I am using three nested for-loops e.g.

Sketch of the For-Loop:

for(const page in object){
  for(const group in page){
    for(element in elements){
       }
      }
    }

I need to visit every element of each elements for each group a page has.

JS Object:

{
"Page 1":{
    "Group 1": {
        "Elements": [
            "Element 1",
        ]
    },
    "Group 2": {
        "Elements": [
            "Element 1"
        ]
    }
},
"Page 2":{
    "Group 1": {
        "Elements": [
            "Element 1",
            "Element 2"
        ]
    }
}

}

Is it O(n) due to the fact that I am visiting each elements only a single time?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The complexity is O(P+G+E) where

  • P stands for the number of pages
  • G stands for the total number of groups
  • E stands for the total number of elements.

In practice this is equivalent to O(E), but if you would have empty pages and/or empty groups (so without elements), then either P or G could be greater than E, and then it is important to speak of O(P+G+E).

If however it is guaranteed that every page has at least one group, and every group has at least one element, then E is the greatest among (P, G, E), and so O(P+G+E) = O(E).

about 4 years ago · Santiago Trujillo Relatório
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