4

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?

kglr
  • 394,356
  • 18
  • 477
  • 896
wkong
  • 321
  • 1
  • 7

4 Answers4

4

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]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • 1
    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 06: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 06:54
  • 1
    @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 01:18
3

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2
list = {{a, b}, {f, c}, {d, b}, {e, c}, {a, c}};

Since V 13.1 we have DeleteElements

DeleteElements[list, DeleteDuplicatesBy[Last] @ list]

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

DeleteElements preserves the ordering, but gets very slow with long lists

eldo
  • 67,911
  • 5
  • 60
  • 168
1
list = {{a, b}, {f, c}, {d, b}, {e, c}, {a, c}};

Using SequenceSplit:

Catenate@Rest@SequenceSplit[list, s : {a_, b_ ..} :> s]

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

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44