I have a list with repeated elements, such as
list = {a, a, b, c, c, c}
and I'd like a list of the unique ways to choose 3 elements from it:
{{a, a, b}, {a, a, c}, {a, b, c}, {a, c, c}, {b, c, c}, {c, c, c}}
Alas, "unique" means two different things at once in that sentence, and I can't figure out how to achieve both types of uniqueness simultaneously.
I could use Permutations
, whose documentation indicates regarding the input that
Repeated elements are treated as identical.
But I will have many results that differ only by rearrangement, and I do not care about order:
Permutations[list, {3}]
{{a, a, b}, {a, a, c}, {a, b, a}, {a, b, c}, {a, c, a}, {a, c, b}, {a, c, c}, {b, a, a}, {b, a, c}, {b, c, a}, {b, c, c}, {c, a, a}, {c, a, b}, {c, a, c}, {c, b, a}, {c, b, c}, {c, c, a}, {c, c, b}, {c, c, c}}
To eliminate the rearrangements, I could try using Subsets
instead, but per its documentation,
Different occurrences of the same element are treated as distinct.
As a result I get many duplicate results that I don't want due to the repeated elements of list
:
Subsets[list, {3}]
{{a, a, b}, {a, a, c}, {a, a, c}, {a, a, c}, {a, b, c}, {a, b, c}, {a, b, c}, {a, c, c}, {a, c, c}, {a, c, c}, {a, b, c}, {a, b, c}, {a, b, c}, {a, c, c}, {a, c, c}, {a, c, c}, {b, c, c}, {b, c, c}, {b, c, c}, {c, c, c}}
[Frustrated aside: I can't begin to imagine why Mathematica's permutations-generating function treats repeated list items differently than its combinations-generating function.]
I could eliminate the duplicates from either result, but either way, that still requires calculating the full list of nonunique results as an intermediate step, which I expect to be many orders of magnitude longer than the unique results.
Is it possible to get the result I'm after without having to cull a humongously longer list first to get there? The full problem I am working toward would be a list of 100 elements, ~25 unique elements with multiplicities ranging between 1 and 12, and desired subsets of 7 elements. (100 choose 7) is 16 billion, hence my interest in avoiding computing the full nonunique subset list.
DeleteDuplicates
on your input and calledSubsets
on that? $\endgroup$Sort@RandomChoice[Range[1,25], 100]
whose largestTally
was 9. There were approximately 1.8 million subsets generated. $\endgroup$