# Problem with Union and Intersection

Consider the following:

list={{a,b,c},{c,d,e},{d,e,f},...{x_,y_,z_}};


I would like to apply Union on the elements of the list in the following way

Union[{a,b,c},{c,d,e},{d,e,f},...{x_,y_,z_}];


The problem Union[list] does not return the desired result when applied on list. Please consider the following example:

list={{10,2,3},{2,3,4},{2,3,50}};
Union[list]


The same problem occures with Intersection.

(* Out={{10,2,3},{2,3,4},{2,3,50}} *)


Union[{10,2,3},{2,3,4},{2,3,50}]
(* Out={2,3,4,10,50} *)

-
You're looking for Apply? – Rojo Apr 19 '12 at 21:50
Union@Flatten@list – F'x Apr 19 '12 at 21:51
Applyworks. Many thanks – John Apr 19 '12 at 21:52

You're looking for Apply

Apply[Union, list]


which can be written in short form as

Union@@list

-

Just stir things up, you can also do the following:

Union[Sequence@@list]


It uses the same function referenced (Map), but makes forces the problem to look like you were expecting:

Union[{a,b,c},{c,d,e},{d,e,f},...{x_,y_,z_}];

-

Union eliminates duplicate elements in a list, or duplicate sublists in a list of lists.

In[9]:= list = {{a, a, a}, {a, a, a}, {d, e, f}};
Union[list]

Out[10]= {{a, a, a}, {d, e, f}}


list={{10,2,3},{2,3,4},{2,3,50}};

even though parts of the sublists are the same, no sublist was a complete duplicate of any other sublist. So there was nothing for Union to do.