Imagine having a basic array like
["apple", "banana", "lemon", "mango"];
.
For example, if we were to find the straightforward hand-rolled combinations of this array, selecting 3 items, but allowing for repetition:
let array = ["apple", "banana", "lemon", "mango"];
for (let i = 0; i < array.length; i++)
for (let j = 0; j < array.length; j++)
for (let k = 0; k < array.length; k++)
console.log(`${array[i]} ${array[j]} ${array[k]}`);
Similarly, creating permutations with no repetition from this array, choosing 3 elements:
let array = ["apple", "banana", "lemon", "mango"];
for (let i = 0; i < array.length - 2; i++)
for (let j = i + 1; j < array.length - 1; j++)
for (let k = j + 1; k < array.length; k++)
console.log(`${array[i]} ${array[j]} ${array[k]}`);
Now the question arises: is there a similarly straightforward method to generate:
- Permutations with no repetition
- Combinations allowing for repetition
In essence, is there a simple, non-recursive or intricate iterative way, akin to the examples above, to produce these other two variations?