最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Finding all permutations of array elements as concatenated strings - Stack Overflow

programmeradmin5浏览0评论

I am trying to write a JavaScript function that returns all binations of the elements of an array of unknown length. The argument to be passed to the function should be an array of single digit strings e.g. [ "5", "7", "9" ].

An example to illustrate the desired functionality:

  • If you pass in an array of [ "5", "7", "9" ], it should return an array with all the possible 3-digit binations of those 3 numbers i.e. [ "579", "759", "957", "795",].
  • If you passed in an array of [ "2", "5", "4", "6" ], you would get the 4-digit binations of those 4 numbers, i.e. [ "2546", "2654", "2465",].
  • If you passed in an array of length 5, you would get the 5-digit binations of those 5 numbers, and so on.
  • If the inputted array has the same digit multiple times, that number should appear multiple times in the resulting array e.g. an input of [ "5", "5", "6" ] produces an output of [ "556", "655", "565",].

I have looked around and it seems that recursion might be the way to go, but I am struggling to get it working. I have attempted the below solution which currently works for 3-digit numbers but I can’t figure out how to make a function which works with an array of unknown length.

function getDoubleDigitCombinations(input) {
  let result = [];
  const first = input[0];
  const last = input[1];

  result.push(first + last);
  result.push(last + first);

  return result;
}


function getTripleDigitCombinations(input) {
  let result = [];
  let main; // This is the number in question.
  let other1; // These are the other numbers.
  let other2;

  for (i = 0; i < input.length; i++) {
    let arr = input.slice(); // Make a copy of the input array.
    
    main = input[i];
    arr.splice(i, 1); // Remove the main element from the array …
    other1 = arr[0]; // … so that you are left with the other two numbers.
    other2 = arr[1];

    console.log(`Main is ${main}`);
    console.log(`other1 is ${other1} and other2 is ${other2}`);

    const arr2 = getDoubleDigitCombinations([other1, other2]); // Get the binations of the others.

    result.push(main + arr2[0]); // Concatenate main with both of the others binations.
    result.push(main + arr2[1]);
  }

  return result;
}

let result2 = getTripleDigitCombinations([ "6", "7", "8" ]);

console.log("result2 is ...");

for (i = 0; i < result2.length; i++) {
  console.log(result2[i]);
}

I am trying to write a JavaScript function that returns all binations of the elements of an array of unknown length. The argument to be passed to the function should be an array of single digit strings e.g. [ "5", "7", "9" ].

An example to illustrate the desired functionality:

  • If you pass in an array of [ "5", "7", "9" ], it should return an array with all the possible 3-digit binations of those 3 numbers i.e. [ "579", "759", "957", "795",].
  • If you passed in an array of [ "2", "5", "4", "6" ], you would get the 4-digit binations of those 4 numbers, i.e. [ "2546", "2654", "2465",].
  • If you passed in an array of length 5, you would get the 5-digit binations of those 5 numbers, and so on.
  • If the inputted array has the same digit multiple times, that number should appear multiple times in the resulting array e.g. an input of [ "5", "5", "6" ] produces an output of [ "556", "655", "565",].

I have looked around and it seems that recursion might be the way to go, but I am struggling to get it working. I have attempted the below solution which currently works for 3-digit numbers but I can’t figure out how to make a function which works with an array of unknown length.

function getDoubleDigitCombinations(input) {
  let result = [];
  const first = input[0];
  const last = input[1];

  result.push(first + last);
  result.push(last + first);

  return result;
}


function getTripleDigitCombinations(input) {
  let result = [];
  let main; // This is the number in question.
  let other1; // These are the other numbers.
  let other2;

  for (i = 0; i < input.length; i++) {
    let arr = input.slice(); // Make a copy of the input array.
    
    main = input[i];
    arr.splice(i, 1); // Remove the main element from the array …
    other1 = arr[0]; // … so that you are left with the other two numbers.
    other2 = arr[1];

    console.log(`Main is ${main}`);
    console.log(`other1 is ${other1} and other2 is ${other2}`);

    const arr2 = getDoubleDigitCombinations([other1, other2]); // Get the binations of the others.

    result.push(main + arr2[0]); // Concatenate main with both of the others binations.
    result.push(main + arr2[1]);
  }

  return result;
}

let result2 = getTripleDigitCombinations([ "6", "7", "8" ]);

console.log("result2 is ...");

for (i = 0; i < result2.length; i++) {
  console.log(result2[i]);
}

Share Improve this question edited Sep 23, 2021 at 21:11 Sebastian Simon 19.6k8 gold badges61 silver badges84 bronze badges asked Feb 8, 2021 at 20:21 bunstrbunstr 331 silver badge4 bronze badges 4
  • Wele to StackOverflow! What would you expect to happen if there were repeated digits? Would there be repeated binations in the answers, or should each appear only once? – Scott Sauyet Commented Feb 8, 2021 at 21:38
  • 1 BTW, you should search the site for relevant answers. Do any of these results help you? – Scott Sauyet Commented Feb 8, 2021 at 22:01
  • @ScottSauyet Good point. It should be repeated in the output array. Thank you for pointing that out, I have updated the question to reflect that! I will check out those answers tomorrow, thanks Scott – bunstr Commented Feb 8, 2021 at 22:19
  • If you’re looking for a non-“concatenated” variant of permutations, i.e. from an array [ "5", "7", "9" ] generating [ [ "5", "7", "9" ], [ "7", "5", "9" ], [ "9", "5", "7" ],], see Permutations in JavaScript?. – Sebastian Simon Commented Sep 23, 2021 at 21:13
Add a ment  | 

4 Answers 4

Reset to default 3

A fun problem! I wanted to implement using generators. This allows you to work with the permutations one-by-one as they are generated, rather than having to pute all permutations before the entire answer is provided -

const input =
  ["
发布评论

评论列表(0)

  1. 暂无评论