2
$\begingroup$

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?

$\endgroup$
3
$\begingroup$

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}}

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

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]
$\endgroup$
  • 1
    $\begingroup$ 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] $\endgroup$ – Lukas Lang Jul 13 '17 at 6:51
  • $\begingroup$ @Mathe172 I am aware of the your 1st assertion, but it not clear to me the 2nd applies. $\endgroup$ – m_goldberg Jul 13 '17 at 6:54
  • $\begingroup$ @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! $\endgroup$ – wkong Jul 14 '17 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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