Skip to content

Instantly share code, notes, and snippets.

@sylvainpolletvillard
Last active February 14, 2016 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sylvainpolletvillard/05da2047e74442522ee9 to your computer and use it in GitHub Desktop.
Save sylvainpolletvillard/05da2047e74442522ee9 to your computer and use it in GitHub Desktop.
array permutations
function getPermutations(arr){
var permutations = [];
(function _(stack, rest){
rest.length
? rest.map((el, i) => _([...stack, el], rest.filter((x,j) => i!=j)))
: permutations.push(stack)
})([], arr);
return permutations
}
// example
getPermutations(['a','b','c']).map(x => x.join(''))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment