Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 06:36
Show Gist options
  • Save hassan-maavan/e35639c61373d84fb50b6136ec243d8f to your computer and use it in GitHub Desktop.
Save hassan-maavan/e35639c61373d84fb50b6136ec243d8f to your computer and use it in GitHub Desktop.
In this kata you have to create all permutations of an input string and remove duplicates, if present. This means, you have to shuffle all letters from the input in all possible orders. https://www.codewars.com/kata/5254ca2719453dcc0b00027d
function permutations(string) {
var arr = string.split(''), tmp = arr.slice(), heads = [], out = [];
if(string.length == 1) return [string];
arr.forEach(function(v, i, arr) {
if(heads.indexOf(v) == -1) {
heads.push(v);
tmp.splice(tmp.indexOf(v), 1);
permutations(tmp.join('')).forEach(function(w) {out.push(v + w);});
tmp.push(v);
}
});
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment