Last active
March 19, 2018 04:08
-
-
Save naveedn/9ad30d2f9bc6789aea24f1e5156d6934 to your computer and use it in GitHub Desktop.
Medium Article Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A slightly different approach that uses different methods to achieve the same result. Just for fun ;) | |
*/ | |
const input = [1,3,5,7]; | |
const fn2 = (inputArr) => { | |
return new Array(inputArr.length) | |
.fill(null) | |
.map(x => [...inputArr]) // if elems in inputArr are not primitive, you will need to deep-copy for the next step | |
.map((x, i) => { | |
x.splice(i,1); // discard removed value | |
return x; | |
}) | |
.map(x => x.reduce((elem, acc) => acc * elem)); | |
} | |
console.log(fn2(input)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Algorithmic Complexity:
Speed: O(n2)
Space: O(n2)
Alternative Approach: https://gist.github.com/naveedn/ede7ccf065a354fc8b0a47e50ef37ab7