Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a unique set S={1,1,3,3,3} but from this unique set I can create many combinations like {1,3,3,3,1}, {3,3,1,3,1}, ... I want to write a generalized formula to represent this. From the below formula I have already tried to find the total combinations:


where 5 is the total number of elements in the set and number of 1's are 2 and number of 3's are 3. But I want to write this formula in a generalized way.

share|improve this question
check out Permutations (and when you are at it, you might like Subsets, Tuples) – Pinguin Dirk Sep 24 '13 at 8:06
I am trying to understand what you are asking for. Do you want the total number of unique permutations of the given set S? – Mr.Wizard Sep 24 '13 at 8:13
yes i wan that..i know how to find this but i want to write it in a generalized formula – user2663126 Sep 24 '13 at 8:18

Since you want to count the elements of each of the classes, use Tally. With Last[#]!&, you define a function that gets the factorials in the denominator:

numberOfCases[s_] := Length[s]!/Times @@ (Last[#]! & /@ Tally[s])
numberOfCases[{a, a, b, a, c, b, a}]


That code instantiates the formula $N!/(n_1!n_2!…n_k!)$ where $N$ is the number of elements of the original set and $n_i$ is the number of elements of the $i$-th class.

share|improve this answer
Well, you beat me. +1 :-) – Mr.Wizard Sep 24 '13 at 8:21
I had too much caffeine today, it is 2:25am here … – Hector Sep 24 '13 at 8:22
I dont want to write any program it's just a theory. so I need a theoretical generalized formula. – user2663126 Sep 24 '13 at 8:23
Then, this is the wrong forum. In any case, I updated my answer to help you out. – Hector Sep 24 '13 at 8:25
@user2663126 I don't understand. You already have a generalized formula, you just need to populate it from your set S. What else do you want if not a program? – Mr.Wizard Sep 24 '13 at 8:27

There is a build-in function Multinomial. So the code can be more compact and clear

numberOfCases2[s_] := Multinomial @@ Last /@ Tally[s]
numberOfCases2[{a, a, b, a, c, b, a}]


From Wikipedia: the multinomial coefficient $$ {n\choose k_1,k_2,\ldots,k_r} =\frac{n!}{k_1!k_2!\cdots k_r!} $$ is the number of distinct ways to permute a multiset of $n$ elements, and $k_i$ are the multiplicities of each of the distinct elements.

share|improve this answer

Just a reap sow example (noting the discussion may render no relevance to answer):

noc[u_] := 
 Divide @@ ({#1!, Times @@ Map[Factorial, #2]} & @@ 
    Reap[Total@(Sow[1, #] & /@ u), _, (Total@#2 &)])

noc[{a, a, b, a, c, b, a}]



noc[S] yields 10

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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