permutation javascript
function getArrayMutations(arr, perms = [], len = arr.length) {
if (len === 1) perms.push(arr.slice(0))
for (let i = 0; i < len; i++) {
getArrayMutations(arr, perms, len - 1)
len % 2 // parity dependent adjacent elements swap
? [arr[0], arr[len - 1]] = [arr[len - 1], arr[0]]
: [arr[i], arr[len - 1]] = [arr[len - 1], arr[i]]
}
return perms
}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,
])
),
[]
);
};
Also in JavaScript:
- swift append to array
- ovelay text on image in react native
- how to add json datasource in jasperserver
- redirect browser javascript
- error: Error: Unable to resolve module `crypto` from `node_modules\crypto-js\core.js`: crypto could not be found within the project.
- js get base url
- express hello world
- jquery check if all checkbox is not checked
- toast notification angular bootstrap 8
- javascript uncheck radio button
- this.setstate is not a function in react native
- how many threads does node js use
- javascript dom cheat sheet
- timestamp to unix time react
- javascript multiline string
- dynamic object property name javascript
- Module Error (from ./node_modules/eslint-loader/dist/cjs.js):
- js variable for key obj
- jquery growl cdn
- javascript array remove first
- get json data when we get error code in axios
- how to find length of string in javascript without using length method
- discord .js embed
- react web worker