Skip to content

Instantly share code, notes, and snippets.

@naveedn
Last active March 19, 2018 04:08
Show Gist options
  • Save naveedn/9ad30d2f9bc6789aea24f1e5156d6934 to your computer and use it in GitHub Desktop.
Save naveedn/9ad30d2f9bc6789aea24f1e5156d6934 to your computer and use it in GitHub Desktop.
Medium Article Snippet
/**
* 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));
@naveedn
Copy link
Author

naveedn commented Mar 19, 2018

Algorithmic Complexity:

Speed: O(n2)
Space: O(n
2)

Alternative Approach: https://gist.github.com/naveedn/ede7ccf065a354fc8b0a47e50ef37ab7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment