0% found this document useful (0 votes)
25 views

Permutations - 30 Seconds of Code

This document provides a JavaScript function to generate all permutations of an array's elements, including duplicates. The function uses recursion to iterate through each element, generate partial permutations of the remaining elements, and combine them with the current element. It has base cases for arrays of length 1 or 2. The function runs exponentially slower as the array length increases, and may cause performance issues for arrays longer than 8-10 elements.

Uploaded by

ranjana deore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Permutations - 30 Seconds of Code

This document provides a JavaScript function to generate all permutations of an array's elements, including duplicates. The function uses recursion to iterate through each element, generate partial permutations of the remaining elements, and combine them with the current element. It has base cases for arrays of length 1 or 2. The function runs exponentially slower as the array length increases, and may cause performance issues for arrays longer than 8-10 elements.

Uploaded by

ranjana deore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

30 seconds of code

 Search...
Home / JavaScript / Array / permutations

permutations
 JavaScript, Array, Algorithm, Recursion
Generates all permutations of an array's elements (contains duplicates).
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.

Base cases are for Array.prototype.length equal to 2 or 1 .


⚠️WARNING: This function's execution time increases exponentially with each array
element. Anything more than 8 to 10 entries may cause your browser to hang as it tries to
solve all the different combinations.
JavaScript
const permutations = arr => {

if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;

return arr.reduce(

(acc, item, i) =>

acc.concat(

permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [

item,
...val,

])
),

[]

);

};

Examples
permutations([1, 33, 5]);

// [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]

  

Recommended snippets

haveSameContents
 JavaScript, Array
Checks if two arrays contain the same elements regardless of order.

isContainedIn
 JavaScript, Array
Checks if the elements of the first array are contained in the second one regardless of order.

uniqueSymmetricDifference
 JavaScript, Array
Returns the unique symmetric difference between two arrays, not containing duplicate
values from either array.
About Cookies RSS GitHub Twitter
Website, name & logo © 2017-2022 30 seconds of code
Individual snippets licensed under CC-BY-4.0
Powered by Netlify, Next.js & GitHub

You might also like