# How do I count the number of duplicates in anydice?

Let's say I have the following roll: 6 6 5 5 5 4. I want to count the number of duplicates, in this case 3 (6 and 5 5).

Is there an intuitive way to model that using Anydice for arbitrary pool sizes or die sides?

Also, is there a way to mathematically deduce it?

Anydice doesn't have a good built in function which can do this, but it's relatively easy to write a program which can:

function: dupes in DICE:s {
D: 0
loop X over {2..#DICE} {
if ((X-1)@DICE = X@DICE) { D: D + 1}
}
result: D
}


Which will work for any size of die and dice pool, subject to Anydice's normal constraints on processing time (in testing the above capped out at 15d6 and 8d10 pools).

This works by taking advantage of Anydice's semantics when casting dice pools to sequences. When a dice pool is converted to sequence by a function, it produces a sequence representing the result of the roll, sorted from highest to lowest result (so 5d6 might give {6,6,4,3,1} but never {6,4,6,1,3}). As we have a sorted sequence we just iterate over it comparing each value to the previous value; if they are equal, there is a duplicate.