I couldn't manage to find a solution this.

I'm not very experienced with mathemathica, so the solution might be quite simple, I don't know.

I have this non sorted set of tuples:

Tuples[{Range[1,4], Range[1, 6], Range[1,8], Range[1, 12], Range[1,20]}]

That represent dice events for 5 dices with a different amount of sides.

Now to the problem:

I'm trying to pattern match the case where there is exactly 1 matching pair of values in a tuple, and the rest of the values are unique

I've started to write a pattern matching function like this:

pairQ[{__, x_, x_, x_, ___} := False;

But using this method to account for every case would need A LOT of lines, surely there must be a much simpler way.

So my question is: How can I efficiently find tuples from this set of length 5 tuples that have 2 values that are equal, rest unique?


2 Answers 2



tuples = Tuples[{Range[1,4], Range[1, 6], Range[1,8], Range[1, 12], Range[1,20]}]

Select[tuples, Length[Union[#]] == 4 &]

will give you the required answer in no time at all.

  • $\begingroup$ Excellent, thank you for a nice and quick answer. $\endgroup$ Commented Aug 24, 2016 at 19:17

It seems that I'm a bit late here, but there's a solution using your own method of pattern matching:


OrderlessPatternSequence is a pattern specially designed for similar cases where people want to ignore the order of elements in a sequence. And in your case, you want to ignore the order between two ___ and two x_, so this will be optimal!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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