# How to count the highest number of matching results?

For a Xd6 dice pool I want to know probabilities of getting a specific number of same outcomes, for all numbers from 2 to X.

For example, I roll 5d6 and get 2, 3, 2, 2, 4 – the result is "3" because there are three 2s. But what was the chance of getting "3" for the 5d6 pool?

For 1, 6, 1, 6, 1 – two 6s, three 1s – the result should be "3", because 3 is the highest.

For 2, 2, 3, 3, 6 the result should be "2", et cetera.

The matching outcome itself is irrelevant, all I want to know is the highest duplicates number. I’ve found this question – How do I count the number of duplicates in anydice? – but it counts all duplicates, not the highest number of them.

Here's a function to count duplicates in a roll:

function: count highest dupes in DICE:s {
MAX: 0
loop X over {1..1@DICE} {
COUNT: X = DICE
if COUNT > 1 & COUNT > MAX { MAX: COUNT }
}
result: MAX
}


Let's step through the function.

function: count highest dupes in DICE:s {


The function declaration takes one parameter, the dice pool rolled, and casts it to a fixed sequence for inspection.

  MAX: 0


Initialises a counter variable, MAX, to remember what the maximum number of duplicates is.

  loop X over {1..1@DICE} {


Loops a variable X over the sequence of values from 1 to the highest individual die result in the pool. Thanks to the automatic sorting of dice cast to a sequence, we know that the first number in the sequence (1@DICE) will be the highest die, and we don't need to check any numbers higher than that.

    COUNT: X = DICE
if COUNT > 1 & COUNT > MAX { MAX: COUNT }


For each value of X, count the number of matches in the pool. If there is more than one match and the number of matches is higher than the currently known maximum, set that to be the new maximum.

  }
result: MAX
}


When the loop ends, the result is whatever the maximum number of duplicates was.

Your specific case of rolling 5d6 would be invoked by:

output [count highest dupes in 5d6]

• You could optimize your code slightly by starting the range at (#DICE)@DICE and dropping the redundant COUNT > 1 condition (which will always be true if COUNT > MAX anyway). With those optimizations, the main difference between our answers is that I'm looping over all the numbers actually rolled (even if some are duplicates) whereas you're looping over all numbers from the lowest roll (or 1, in your current code) up to the highest roll, even if some don't appear on any die. Oct 1, 2021 at 19:04
• … Which one's faster will depend on the number of dice rolled and the number of sides on them; mine will in general be faster for small numbers of dice with many sides, while yours will be faster for large numbers of dice with few sides. For something like 5d6, they should be pretty close. Oct 1, 2021 at 19:05
• (Um, wait, let me amend my comment above: COUNT > 1 isn't strictly redundant in your code, since you start MAX at 0. However, the only effect of including it is that the result for non-empty dice pools with all rolls distinct will be 0 instead of 1. While there is in fact an argument to be made that such rolls don't have any "matching results", I'd still consider 1 a more logical result than 0 in that case. In any case, the plots produced by your code and mine look the same: the only difference is whether the lowest bar is labelled 0 or 1.) Oct 1, 2021 at 19:14
• Thank you for guiding through the function. Despite my familiarity with a few programming languages, the AnyDice script remains inconvenient and very counter-intuitive. Oct 2, 2021 at 21:08

I have a strange feeling that this has been asked before, but I couldn't find the duplicate, so here's an answer:

function: highest number of matches in ROLL:s {
MAX: 0
loop I over ROLL {
COUNT: ROLL = I
if MAX < COUNT { MAX: COUNT }
}
result: MAX
}

output [highest number of matches in 5d6]

• And here I was drafting my answer, thinking to myself "I got here before Ilmari did..." Oct 1, 2021 at 19:03
• BTW, I think this is the (near) duplicate question I was thinking of, but it's not actually the same question; it's asking for the highest sum of matching results in a roll, not the highest number of matching results. The solution is very nearly the same, though. Oct 2, 2021 at 15:23