I did a coding challenge requiring to merge an input of arrays into a new array by adding only once the duplicated elements and keeping the order of the array elements. My solution is below:
function union(arr){
let newArr = [];
let length = arr.length;
for(let i=0; i< length; i++){
for(let j=0; j<arr[i].length; j++){
if(!newArr.includes(arr[i][j])){
newArr.push(arr[i][j]);
}
}
}
return newArr;
I wanted to know how can I improve the big O performance of the above solution without using any javascript build in methods as reduce or map etc. Is there a way without using nested loops?