如何计算以下 JavaScript 算法的运行时复杂度?
How do I calculate runtime complexity of the following JavaScript algorithm?
下面是一个合并 2 个数组的简单程序。我使用了两种不同的方法来解决这个问题。如何计算这两个版本的运行时复杂度(如果可能的话 space 复杂度)?提前致谢!
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);
答案就在一个叫做 Big O Notation 的术语中。您可以在下面找到详细的文章。
此外,您可以使用函数式编程工具 Ramda.js 来获得相同的结果,即获取唯一编号。
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);
下面是一个合并 2 个数组的简单程序。我使用了两种不同的方法来解决这个问题。如何计算这两个版本的运行时复杂度(如果可能的话 space 复杂度)?提前致谢!
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);
答案就在一个叫做 Big O Notation 的术语中。您可以在下面找到详细的文章。
此外,您可以使用函数式编程工具 Ramda.js 来获得相同的结果,即获取唯一编号。
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);