Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!