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

192
Visualizações
How to change a List to a Dictionary having the values as Boolean

I have the following code. In this case I have a Big O(n^2). How can I write this code in a better way using another Big O Notation? The code is suppose to return True if one value from the list matches one value from another, else it should return False.

l1 = [1, 2, 3, 4]
l2 = [1, 6, 7, 8]

def common_inputs(list1, list2):
  for i in l1:
    for j in l2:
      if i == j:
        return True
      else:
        return False


print(common_inputs(l1, l2))

I have an example but is written in JavaScript and can't understand that much.

array1 = [1, 2, 3, 4]
array2 = [1, 6, 7, 8]

function commonInputs(arr1, arr2) {
  let map = {};
  for (let i=0; i < arr1.length; i++) {
    if (!map[i]) {
      const item = arr1[i];
      map[item] = true;
    }
  }

  for (let j=0; j < arr2.length; j++) {
    if (map[arr2[j]]) {
        return true;
    }
  }
  return false;
}

Any advice will be appreciated. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The python idiom to test if two sets intersect would be

intersect = not set(a1).isdisjoint(a2)

In javascript there's no such thing, so you'll have to loop the second list:

function intersect(a1, a2) {
    let s1 = new Set(a1)
    return a2.some(x => s1.has(x))
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use set for more pythonic way.

l1 = [1, 2, 3, 4]
l2 = [1, 6, 7, 8]
common = set(l1) & set(l2)
len(common) > 0
>> True
about 4 years ago · Juan Pablo Isaza 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