I'm trying to develop a JavaScript app that needs a certain algorithm to work, but I have not been able to figure it out.I believe the task I have to perform is better explained with an example:
Let's say I have a list of letters:
$\begin{bmatrix}a & b & c & d\end{bmatrix}$
Note that, in my application, the number of letters $n$, is always in $[0, 9]$, and that each letter is picked randomly from a pool of 8 letters.
From there, I can get all 2-combinations (with repetition) of the list:
$\begin{bmatrix}a & b\end{bmatrix}$ $\begin{bmatrix}a & c\end{bmatrix}$ $\begin{bmatrix}a & d\end{bmatrix}$ $\begin{bmatrix}b & c\end{bmatrix}$ $\begin{bmatrix}b & d\end{bmatrix}$ $\begin{bmatrix}c & d\end{bmatrix}$ $\begin{bmatrix}a & a\end{bmatrix}$ $\begin{bmatrix}b & b\end{bmatrix}$ $\begin{bmatrix}c & c\end{bmatrix}$ $\begin{bmatrix}d & d\end{bmatrix}$
Now I want to get all 2-combinations from this list of combinations, but with one constraint: each letter can only be used up to a number of times, and this number can change. Let's say for example, we set that all letters can only appear once, so you could make:
$\begin{bmatrix}a & b\end{bmatrix}$ and $\begin{bmatrix}c & d\end{bmatrix}$
But not:
$\begin{bmatrix}a & b\end{bmatrix}$ and $\begin{bmatrix}a & c\end{bmatrix}$
Because $a$ appears in both elements.
Also note that in this example $n=4$, so you can at most pick two pairs of items. But if $n$ is greater, the algorithm should return three pairs of letters in each combination for $n>=6$ and four pairs for $n>=8$.
But let's say we now set that $a$ can appear up to two times, and $ b c d$ can only appear once each. So now, the combination $\begin{bmatrix}a & b\end{bmatrix}$ and $\begin{bmatrix}a & c\end{bmatrix}$ would be valid, and also the combination $\begin{bmatrix}a & a\end{bmatrix}$ and $\begin{bmatrix}b & c\end{bmatrix}$, but not $\begin{bmatrix}a & a\end{bmatrix}$ and $\begin{bmatrix}b & b\end{bmatrix}$ because $b$ can only appear once.
As you can see, there are multiple sets of combinations that meet the criteria, and in fact the point of the algorithm is to calculate ALL sets that meet the criteria, not just one.
I also noticed that when you have an odd number of letters, there will be one leftover letter in each set of combinations. I also need the algorithm to tell me which letter was leftover in each set of combinations, but I believe this won't be a problem because you can substract the combinations from the original list.
The only way I've managed to do this is to calculate all possible combinations, then filter out the ones that don't comply with the constraints, but this seems very inefficient and looks like there has to be a better way to do it. Any ideas?
Thanks in advance.
EDIT: I'm sorry for all the important information that I unknowingly omitted.I'm adding it as soon as I see someone requesting it. In case it helps anyone, my application is in fact not about letters, but about Teamfight Tactics (a videogame) items. If anyone is familiar with the game, that should help them better understand my question. I didn't want to say it initially because I thought I should only talk about the mathematics here, but I now see it could help clarify the problem.