From d995044ead0be7003111a0b9b8c3f6c93621941a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Nov 2019 22:01:52 +0200 Subject: [PATCH] Update permutations.md --- snippets/permutations.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/permutations.md b/snippets/permutations.md index 331c8af37..7252abecc 100644 --- a/snippets/permutations.md +++ b/snippets/permutations.md @@ -3,10 +3,10 @@ title: permutations tags: array,recursion,advanced --- -⚠️ **WARNING**: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations. - Generates all permutations of an array's elements (contains duplicates). +⚠️ **WARNING**: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations. + Use recursion. For each element in the given array, create all the partial permutations for the rest of its elements. Use `Array.prototype.map()` to combine the element with each partial permutation, then `Array.prototype.reduce()` to combine all permutations in one array. @@ -27,4 +27,4 @@ const permutations = arr => { ```js permutations([1, 33, 5]); // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ] -``` \ No newline at end of file +```