theusual
theusual

Reputation: 791

How do I calculate runtime complexity of the following JavaScript algorithm?

Below is a simple program to merge 2 arrays. I have used two different ways to solve this problem. How do I calculate runtime complexity, if possible space complexity too, of both these versions? Thanks in advance!

let A = [1, 2, 3];
let B = [2, 3 , 4, 5];

// VERSION: 1
//let C = A.concat(B.filter( item => {return A.indexOf(item) < 0;} ));

// VERSION: 2
let C = [...new Set([...A,...B])];

// result:
console.log(C);

Upvotes: 0

Views: 4543

Answers (1)

agriboz
agriboz

Reputation: 4854

The answer is lies within a term called Big O Notation. You can find detailed articles below.

In addition, you can use functional programming tools Ramda.js that achieve the same result which is getting the uniq numbers.

let A = [1, 2, 3];
let B = [2, 3 , 4, 5];
const concat = R.concat(A,B);
const result = R.uniq(concat);

console.log(result);

For example

Time Complexity Big O Notation

Upvotes: 2

Related Questions