Horje
array permutation Code Example
permutation javascript
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,
        ])
      ),
    []
  );
};
array permutation
const permute = (input = [], permutation = []) => {
    if (input.length === 0) return [permutation]; // this will be one of the result

    // choose each number in a loop
    return input.reduce((allPermutations, current) => {
        // reduce the input by removing the current element
        // as we'll fix it by putting it in `permutation` array
        const rest = input.filter(n => n != current);
        return [
            ...allPermutations,
            // fixing our choice in the 2nd arg
            // by concatenationg current with permutation
            ...permute(rest, [...permutation, current])
        ];
    }, []);
}




Javascript

Related
err handling express Code Example err handling express Code Example
string sort javascript Code Example string sort javascript Code Example
How to add js file to a site through url Code Example How to add js file to a site through url Code Example
disadvantages of array Code Example disadvantages of array Code Example
how to update state.item[1] in state using setState? React Code Example how to update state.item[1] in state using setState? React Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9