Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
What is the BigO of these simple Javascript functions?

I have the following two arrays, names call it n, and sortOrder call it k.

const names = ['bob', 'bob', 'jim', 'mike', 'dale']; // n
const sortOrder = ['jim', 'bob', 'mike']; // k, this is a unique array

the result after my functions should be an array that looks like so,

['jim', 'bob', 'bob', 'mike']

that is ordering the array based on the position of the names in sortOrder.

What is the Big(O) of the following two functions? Thanks in advance!

function order1(names, sortOrder) {
const newNames = [];

for (let order of sortOrder) {
    for (let name of names) {
        if (order === name) {
            newNames.push(item);
            continue;
        }
    }
}

return newNames;
}


function order2(names, sortOrder) {
let newNames = [];
const orderMap = {};

for (let name of names) {
    if (!orderMap[name]) {
        orderMap[name] = 1;
    } else {
        orderMap[name] += 1;
    }
}

for (let order of sortOrder) {
    if (orderMap[order]) {
        newNames = [...newNames, ...Array(orderMap[order]).fill(order)];
    }
}


return newNames;
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda