0
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ @D.W. rephrased the question $\endgroup$
    – lapots
    Nov 4, 2023 at 22:14
  • $\begingroup$ When two records are equal? If they are equal only when every field is equal than in order to guarantee the unicity of records it is suffiecient to guarantee that each set has not repeated elements and, once you removed duplicates inside each set you just need to count the total number of combinations $\endgroup$
    – SilvioM
    Nov 5, 2023 at 13:37
  • $\begingroup$ So basically the same thing that I am doing? Because I generate a combination, put it into a set and repeat that until the set is full. (or at least I am trying to do that for 10m cycles). $\endgroup$
    – lapots
    Nov 6, 2023 at 19:51

1 Answer 1

0
$\begingroup$

Let $n$ be the number of unique names, and $m$ be the number of unique surnames. Since we have $n$ unique names, and each of them may choose $m$ unique surnames, we will have $nm$ unique name-surname combinations.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.