Skip to content

Instantly share code, notes, and snippets.

@robpe
Last active December 19, 2015 15:48
Show Gist options
  • Save robpe/5978962 to your computer and use it in GitHub Desktop.
Save robpe/5978962 to your computer and use it in GitHub Desktop.
array permutations
function ipslice(arr, l) {
var arr = arr.slice()
arr.splice(l, 1)
return arr
}
var permutations = function(arr) {
if(!arr.length) return [arr]
var perm = []
for(var i in arr) {
perms = permutations(ipslice(arr, i))
for(var j in perms)
perm.push([arr[i]].concat(perms[j]))
}
return perm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment