Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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,
        ])
      ),
    []
  );
};
Comment

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])
        ];
    }, []);
}
Comment

PermCheck: Check whether array A is a permutation.

function isPermutation(arr)
{
var isPermutation = true;

arr.sort(function(a, b){return a - b});

for(var i = 0; i<arr.length-1; i++)
{
if(arr[i]+1!=arr[i+1])
{

isPermutation = false;
}

}

return isPermutation;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js reduce method 
Javascript :: htmlfor jsx attr 
Javascript :: puppeteer js headless mode 
Javascript :: How to Check if a Substring is in a String in JavaScript Using the includes() Method 
Javascript :: react native flex 2 columns per row 
Javascript :: delete item from array 
Javascript :: react setstate in another component 
Javascript :: axios npm 
Javascript :: new Map() collection in react state 
Javascript :: JavaScript throw with try...catch 
Javascript :: external css not working in jsp 
Javascript :: split and convert a string into object 
Javascript :: how to prevent xss attacks in node js 
Javascript :: in vs of javascript 
Javascript :: add color to attribute using jquery 
Javascript :: js how to see console day tomorrow 
Javascript :: js range array 
Javascript :: postman environment variables 
Javascript :: date format in moment js 
Javascript :: js convert obj to array 
Javascript :: how to remove first element from array in javascript 
Javascript :: how to check empty object js 
Javascript :: node cron npm how to use 
Javascript :: perent to child data pass in angular 
Javascript :: export excel form angular array to excel 
Javascript :: rich text react renderer 
Javascript :: byte number to array js 
Javascript :: jquery slider move event 
Javascript :: How to add JSX elements in an array 
Javascript :: how to find a name of class from page in jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =