Let's say we have such an array: myArray = [A, A, B, B, C, C, D, E]
I would like to create an algorithm so that it will find all the combinations that add up to the whole array, where none of the elements are repeated.
Example combinations:
[A, B, C, D, E] [A, B, C]
[A, B, C, D] [A, B, C, E]
[A, B, C] [A, B, C] [D, E]
Clarification: [A, B, C] [A, B, C] [D, E]
and [A, B, C] [D, E] [A, B, C]
are the same combinations. Also the ordering with the subsets doesn't matter as well. For example [A,B,C]
and [B,A,C]
should be the same.
So far, I didn't progress beyond
var myArray = ["A", "A", "B", "B", "C", "C", "D", "E"]
console.log([...new Set(myArray)])
But this doesn't help at all, it just returns one distinct set. I couldn't find a similar problem posted before, so could anyone guide me here in how to achieve this?
[a,b,c]
appears two times in your example of combinations. – Isoniazid[A, B, C] [A, B, C] [D, E]
and[A, B, C] [D, E] [A, B, C]
distinct combinations? How about[A, B, C] [A, B, C] [D, E]
and[A, B, C] [C, A, B] [E, D]
? Are singleton sets allowed (e.g.,[A] [A] [B] [B] [C] [C] [D] [E]
)? – Viscous[A,B] [A,B,C] [C,D,E]
seems a perfectly legitimate combination according to your question, but it is not produced by the solution of @Mr. Polywhirl. If it is legitimate, the answer of @Mr. Polywhirl is wrong. If it is not, you should explain why, and edit your question. – Krenek[AB] [A] [B] [C]...
and so on, it doesn't produce the desired result, so I can safely ignore those possiblities. The broader context is not necessary to explain right now I think, but for what I asked above, your answer is the most precise one right now :) – Epicene[A]
, even if there is only one of them, which leaves us with 37 possibilities. Did I get it correctly? – Krenek