I have two very simple arrays, one containing characters
, and the other containing numbers
. Without repeating any values from the numbers
array, I need to find all possible combinations of the array values. Given the following two data sets:
char[] characters = { 'a', 'b' };
int[] numbers = { 1, 2, 3 };
The expected output is:
1a2a3
1a2b3
1b2a3
1b2b3
If a number is repeated, it should be counted a second time. For example, if we change the numbers
data set to 1, 2, 2
the expected output is:
1a2a2
1a2b2
1b2a2
1b2b2
I've looked at many examples that demonstrate using recursion (and in some cases Linq was used), to find all possible combinations of the values in one array, and even a few samples of across two, but in all of the samples I've seen, the resulting data set was a 1 to 1 mapping such as a1, b1, a2, b2, a3, b3
, which is not what I need.
I've tried few different approaches, but none have worked, nor were they scalable so there's no need to post those here as I abandoned them all along the way. Think along the lines of:
foreach (char character in characters) {
foreach (int number in numbers) {
for (int i = characters.Length - 1; i > 0; i--) {
// Not scalable, probably why recursion is used as a solution.
}
}
}
I'm having trouble getting my brain to move this to recursion for some reason.
How do I create all possible combinations of the values in two arrays, where the length of both arrays are unknown, and the values in one array are not repeated (unless specified in the dataset itself)?
NOTE: The results should be stored in a List<string>
for later processing.