# Listing what has been deleted by DeleteDuplicatesBy

How can I list the items deleted from DeleteDuplicatesBy?

For example:

DeleteDuplicatesBy[{{a, b}, {f, c}, {d, b}, {e, c}, {a, c}}, Part[#, 2] &]


gives

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

However, is there any simple way to get the list {{d, b}, {e, c}, {a, c}} of deleted pairs?

Based on @Coolwater's fine answer to Replace element in array by checking condition in another list you can use GatherBy:

Catenate @ GatherBy[
{{a,b},{f,c},{d,b},{e,c},{a,c}},
Last
][[All, 2;;]]


{{d, b}, {e, c}, {a, c}}

update

Here's another very similar version:

Catenate @ GroupBy[
{{a,b},{f,c},{d,b},{e,c},{a,c}},
Last,
Rest
]


{{d, b}, {e, c}, {a, c}}

• Thanks a lot! This would be very useful to me. – wkong Jul 14 '17 at 1:17

Here is one way.

data = {{a, b}, {f, c}, {d, b}, {e, c}, {a, c}};
p1 = DeleteDuplicatesBy[data, Part[#, 2] &]


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

p2 = DeleteCases[data, Alternatives @@ p1]


{{d, b}, {e, c}, {a, c}}

Here is another.

p2 = Complement[data, p1]

• Note that both of these solutions fail for data with two identical items. I assume that the goal is to get a list p2 s.t. Sort[p1~Join~p2]==Sort[data] – Lukas Lang Jul 13 '17 at 6:51
• @Mathe172 I am aware of the your 1st assertion, but it not clear to me the 2nd applies. – m_goldberg Jul 13 '17 at 6:54
• @m_goldberg Although there is some problem for two identical items, it is still workable in my case as there is no identical items in my data set. Thanks a lot! – wkong Jul 14 '17 at 1:18