I have 10 words. How can I get all possible combinations of 5 words (n=10, k=5)
. The order does not matter.
For example: "A", "B", "C", if k=2 (n=3 in this case)
, it would like AB, BC and AC. Maybe you know some usefull code or example.
P.S. Sorry if I'm not right enough cause I don't know English very good.
Here's what I put together:
...
produces:
Note that this is concise but inefficient and should not be used for large sets or inner loops. Notably, the short arrays are re-created multiple times and could be memoized, and the
IEnumerable
will be iterated multiple times, which can cause unexpected work if care is not taken.Also, if the input contains duplicates then the output will as well. Either use
.Distinct().ToArray()
first, or use another solution which includes equality checking and, presumably, takes anIEqualityComparer
for generality.What about a more functional solution
Ouput:
What you are trying to do is get all the permutations of a collection.
Here is the code snippet:
Outputs: