Array Arrangement Permutation

前端 未结 4 556
半阙折子戏
半阙折子戏 2021-01-26 18:09

Looking for a way to sequentially find different arrangement possibilities of an array. I only care about adding them sequentially, doesn\'t need skip or shuffle values.

相关标签:
4条回答
  • 2021-01-26 18:15

    You can easily reduce the array by slicing the array to the current index.

    var inputArray = ['a', 'b', 'c', 'd', 'e', 'f'];
    
    var outputArray = inputArray.reduce(function(result, item, index, arr) {
      return result.concat(arr.slice(0, index + 1).join(''));
    }, []);
    
    
    document.body.innerHTML = '<pre>' + outputArray.join('\n') + '</pre>';

    Note: I am still not sure what you mean by "find different arrangement possibilities of an array".

    0 讨论(0)
  • 2021-01-26 18:16

    You could iterate over each character once and should be able to populate all sequences.

    Here is what you could do.

    var inputArray = ['a', 'b', 'c', 'd', 'e', 'f'];
    
    var outputStrings = [];
    
    inputArray.forEach((item, idx) => {
      let prevString = (idx !== 0) ? outputStrings[idx - 1] : "";
      outputStrings.push(prevString + item);
    });
    
    console.log(outputStrings);

    0 讨论(0)
  • 2021-01-26 18:26

    var array = ['a', 'b', 'c', 'd', 'e', 'f'];
    results = [];
    for (x = 1; x < array.length + 1; ++x) {
      results.push(array.slice(0, x).toString().replace(/,/g, ""))
    }
    //PRINT ALL RESULTS IN THE RESULTS VARIABLE
    for (x = 0; x < results.length; ++x) {
      console.log(results[x])
    }

    0 讨论(0)
  • 2021-01-26 18:41

    You need a recursive function to do this.
    Since the amount of possible arrangement of your array is 6! (which is 720), I will shorten it to 3 to shorten the sample result, make the number of possible arrangement to 3! (which is 6)

    var array = ['a', 'b', 'c'];
    var counter = 0; //This is to count the number of arrangement possibilities
    
    permutation();
    console.log(counter); //Prints the number of possibilities
    
    function permutation(startWith){
        startWith = startWith || '';
        for (let i = 0; i < array.length; i++){
            //If the current character is not used in 'startWith'
            if (startWith.search(array[i]) == -1){
                console.log(startWith + array[i]); //Print the string
                    
                //If this is one of the arrangement posibilities
                if ((startWith + array[i]).length == array.length){
                    counter++;
                }
                    
                //If the console gives you "Maximum call stack size exceeded" error
                //use 'asyncPermutation' instead
                //but it might not give you the desire output
                //asyncPermutation(startWith + array[i]);
                permutation(startWith + array[i]);
            }
            else { 
                continue; //Skip every line of codes below and continue with the next iteration
            } 
        }
        function asyncPermutation(input){
            setTimeout(function(){permutation(input);},0);
        }
     }

    The first 3 output is part of your desired output. Hope this answers your question.

    0 讨论(0)
提交回复
热议问题