Let's say I have a set of two columns - Name, Surname. I have a list of possible values for
- Name -> Jacob, John
- Surname -> Mayerson, Kindle.
I want to generate a set of unique combinations for columns Name and Surname. The result looks like this.
--------------------
| Jacob | Mayerson |
| Jacob | Kindle |
| John | Mayerson |
| John | Kindle |
--------------------
The way I currently do that is I generate a pair of (name, surname), put it into a set and if the size of the set increases, it means that the combination is unique and thus I can go and generate the next one. If I generate the combination and it does not increase the size of the set, then I try to generate the next combination until it increases the size of the set.
If user asks for 10 unique rows, then it is obviously impossible to generate 10 unique combinations from the available set of values. But in order tell the user that, right now I am making num_of_requested_rows * 2
attempts to generate a unique combination, before I notify the user that it is impossible to generate the requested amount of rows.
It can be time consuming, because user might wait for minutes before being notified that it is impossible to generate the requested amount of rows.
Thus I have been wondering if there is some more effective way for the generator to fail fast - before even it tries to generate the unique combinations.
I know it is possible to calculate the amount of all possible combinations (permutations basically) using factorials. But the problem is that I can have not just two columns, but three, four, five etc. columns with the potential value set ranging from 10 to thousands of values. For example I can have an integer range from 1 to 100m and so on, while also having a combination with a column that has just a hundred of possible values. And calculating the factorial for such case is also a resource heavy operation.
So my question is - it is even possible to do? Or I have no choice but to use my current approach?