Skip to content

Instantly share code, notes, and snippets.

@viebel
Created September 19, 2016 17:48
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 viebel/5cc67a97903f04036b569c0eb0436e5f to your computer and use it in GitHub Desktop.
Save viebel/5cc67a97903f04036b569c0eb0436e5f to your computer and use it in GitHub Desktop.
// Many thanks to http://stackoverflow.com/questions/9960908/permutations-in-javascript/9960925#9960925
permutations = function(arr){
var permArr = [],
usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
};
return permute(arr);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment