Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to find all permutations of an array with 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
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: laravel array to js 
Javascript :: javascript find the min in array of numbers 
Javascript :: jquery div element find and remove 
Javascript :: react-native android build apk 
Javascript :: create svg element javascript 
Javascript :: eslint-disable-next-line 
Javascript :: update array of object using other array javascript 
Javascript :: js detect link in string 
Javascript :: select2 find option by value 
Javascript :: angular access service in console 
Javascript :: update many mongoose 
Javascript :: disable submit button if input is empty 
Javascript :: nl2br javascript 
Javascript :: return fetch javascript 
Javascript :: how to get items in dynamodb nodejs 
Javascript :: javascript change paragraph text 
Javascript :: convert json string into json object 
Javascript :: access variable outside for loop javascript 
Javascript :: regex link validation 
Javascript :: url regex javascript 
Javascript :: capitalise first letter js 
Javascript :: jquery on change 
Javascript :: stylelint init 
Javascript :: js string have number js 
Javascript :: cypress get element val and return it 
Javascript :: window.location 
Javascript :: js random numbers 
Javascript :: javascript remove all children with class 
Javascript :: get random entry from array javascript 
Javascript :: jquery get padding of element 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =