# Pattern for finding matches that are different [duplicate]

I am trying to solve the following problem. I have tuples of size 3

{{a, b, c}, {a, b, c}, {a, b, c}}


I would like to find cases where

• all the elements in a tuple are the same
• all the elements in a tuple are different

I can solve the first problem with a named pattern something like

x = {{a, b, c}, {a, a, a}, {a, b, c}}
Cases[x, {i_,i_,i_}]

-> {{a, a, a}}


I am unable to solve the other one. Naive approach like this

x = {{a, b, c}, {a, a, a}, {a, b, c}}
Cases[x, {i_,j_,k_}]

-> {{a, b, c}, {a, a, a}, {a, b, c}}


does not work since i, j, k can be different but also can match the same element.

I am looking for a solution using patterns and rules. I could probably construct some solution based on uniqueness of the set but this is a part of larger problem so I am trying to find some elegant solution and also it serves as an exercise to learn more about patterns.

• I am closing this question as already has an answer. (See the link inserted in the header above your question.) All of those methods are applicable here. If you have trouble writing one of them as a pattern please let me know. – Mr.Wizard Oct 15 '14 at 19:10
• Sorry. I were not able to find the aforementioned answer. I think I have more than enough great info to make it work. Thanks – Tomas Svarovsky Oct 15 '14 at 20:04
• No need to be sorry; it is often hard to find prior questions, which is why I work hard to find and link related questions and close as appropriate. I can use all the help I can get so please use the "flag" link below any posts which you feel are duplicates and let the moderators (of which I am one) know about them. – Mr.Wizard Oct 15 '14 at 23:32

A slightly different pattern from Leonid Shifrin's answer

t = Tuples[{a, b, c}, 3];

Length[t]


27

For all of the elements being different

Length[DeleteCases[t, {___, x_, ___, x_, ___}]]


6

• Bob would you mind explaining the idea behind it? Honestly I do not fully grasp how that works. – Tomas Svarovsky Oct 15 '14 at 17:56
• "___ (three _ characters) or BlankNullSequence[] is a pattern object that can stand for any sequence of zero or more Wolfram Language expressions." The pattern represents two identical expressions that are preceded, separated, or followed by zero or more expressions. – Bob Hanlon Oct 15 '14 at 19:42
• Awesome, if I get this right it should work for arbitrary size of a tuple. Thank you. – Tomas Svarovsky Oct 15 '14 at 20:03

There is new DuplicateFreeQ. So

x = {{a, b, c}, {a, a, a}, {a, b, c}, {a, b, b}};

Cases[x, _?DuplicateFreeQ]
(* {{a, b, c}, {a, b, c}} *)

x = {{a, b, c}, {a, a, a}, {a, b, c}, {a, b, b}};

Cases[x, {(y_) ..}]
(* {{a,a,a}} *)
Cases[x, y_?(Length@DeleteDuplicates[#] == 1 &)]
(* {{a,a,a}} *)
Cases[x, y_?(Length@Union[#] == 1 &)]
(* {{a,a,a}} *)

Cases[x, y_?(DeleteDuplicates[#] == # &)]
(* {{a,b,c},{a,b,c}} *)
Cases[x, y_?(Length@Union[#] == Length@# &)]
(* {{a,b,c},{a,b,c}} *)

• DeleteDublicates seems to be a bit faster – ybeltukov Oct 15 '14 at 18:32
• Thank you @ybeltukov; added a variant with DeleteDuplicates. – kglr Oct 15 '14 at 18:58