Need help to optimize the following code for lower time complexity:
arrayMultiply(ipnArray: number[]): number[] {
let outArray = [];
for (let i=0; i<ipnArray.length; i++){
let currentNum = ipnArray[i];
let newArr = ipnArray.filter(nub => nub !== currentNum);
let tempValue;
for (let j=0; j<newArr.length; j++ ){
if (tempValue) {
tempValue = tempValue * newArr[j];
}else{
tempValue = newArr[j];
}
}
outArray.push(tempValue);
}
return outArray;
}
This method takes an input of an array of numbers and returns the array where each element is a product of the other elements even if it has duplicate values. For Example: [1,2,3] becomes [6,3,2] Example 2: [1, 2, 2, 3] will be [12, 6, 6, 4]
Edit: My bad. the return should be [6, 3, 2] instead of the previous typo.
[12, 6, 6, 4]for[1, 2, 2, 3]. \$\endgroup\$