Unique Combinations

This algorithm finds all unique combinations in a specified list of values.



									/*****Please include following header files*****/
// string
// vector
/***********************************************/

/*****Please use following namespaces*****/
// std
/*****************************************/

static vector<vector<string>> GenerateCombinations(vector<string> arr)
{
	vector<vector<string>> combinations;
	int length = arr.size();

	for (int i = 0; i < (1 << length); ++i)
	{
		vector<string> combination;
		int count = 0;

		for (count = 0; count < length; ++count)
		{
			if ((i & 1 << count) > 0)
				combination.push_back(arr[count]);
		}

		if (count > 0 && combination.size() > 0)
			combinations.push_back(combination);
	}

	return combinations;
}
								


Example

									vector<string> arr = { "How", "Are", "You" };
vector<vector<string>> combinations = GenerateCombinations(arr);
								


Output

									{"How"}
{"Are"}
{"How", "Are"}
{"You"}
{"How", "You"}
{"Are", "You"}
{"How", "Are", "You"}