So all the searches I find seem to give results that have unique permutations include the same set of values but in a different order.
So lets say I have an array:
int[] test = {1, 2, 3, 4};
The expected result sets are:
1
1,2
1,2,3
1,2,3,4
1,2,4
1,3
1,3,4
1,4
2
2,3
2,3,4
2,4
3
3,4
4
However, the results I am getting are:
1
1,2
1,2,3
1,2,3,4
2
2,3
2,3,4
3
3,4
4
As you can see, I am missing these results:
1,2,4
1,3
1,3,4
1,4
2,4
The code I am using is:
int[] test = {1, 2, 3, 4};
List<string> results = new List<string>();
for (int i = 0; i < test.Length;)
{
results = prepareResults(test, results, "", test.Length);
if (test.Length > 1)
{
int[] temp = new int[test.Length - 1];
for (int j = 1; j < test.Length; j++)
{
temp[j - 1] = test[j];
}
test = temp;
}
else
{
break;
}
}
public List<string> prepareResults(int[] dataSet, List<string> resultsList, string initialResult, int originalLength)
{
while (dataSet.Length > 1)
{
if (initialResult.Length > 0)
{
initialResult += ",";
}
initialResult += dataSet[0].ToString();
resultsList.Add(initialResult);
int[] temp = new int[dataSet.Length - 1];
for (int j = 1; j < dataSet.Length; j++)
{
temp[j - 1] = dataSet[j];
}
dataSet = temp;
resultsList = prepareResults(dataSet, resultsList, initialResult, originalLength);
return resultsList;
}
if (initialResult.Length != (originalLength * 2) - 1)
{
if (initialResult.Length > 0)
{
initialResult += ",";
}
initialResult += dataSet[0].ToString();
resultsList.Add(initialResult);
}
return resultsList;
}
I am sure that I am just missing something stupid and obvious but I have been staring at this and trying different things for hours now, any suggestions?