Finding tuples with exactly 1 matching pair and rest unique values

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?

Given

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.

• Excellent, thank you for a nice and quick answer. – user3284549 Aug 24 '16 at 19:17

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

Cases[data,{OrderlessPatternSequence[___,___,x_,x_]}]


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!