# How to remove the item that is repeated?

If i have a

list={m,n,p,q,q,r,l,l}


how can i remove the repeated item with pattern matching so i would have

modifiedlist={m,n,p,r}?


Keys@Select[Counts[list], # == 1 &]


{m, n, p, r}

Also:

Select[list, Count[list, #] == 1 &]
Select[list, Counts[list][#] == 1 &]
Flatten[Cases[Split[Sort[list]], {_}]]
Flatten[DeleteCases[Split[Sort[list]], {_, __}]]
Flatten[Select[Split[Sort[list]], Length@# == 1 &]]
SequenceReplace[list, {OrderlessPatternSequence[Repeated[b_, {2, Infinity}], a_]} :> a]

• very nice i added {m,n,p,q,q,r,l,l} /. a_List :> Select[a, Count[a, #] == 1 &] Jan 23, 2019 at 12:26
• @user49047, Select[a, Count[a, #] == 1 &] gives you the result directly; you don't need to use ReplaceAll.
– kglr
Jan 23, 2019 at 12:30
• The method using Counts is probably the best way, since you only have to traverse the whole list once. Jan 23, 2019 at 12:41
• @SjoerdSmit, excellent point.
– kglr
Jan 23, 2019 at 12:43
First /@ Select[Tally@list, Last@# == 1 &]


{m, n, p, r}

or

First /@ Cases[Tally@list, {_, 1}]


{m, n, p, r}

• Cases[] seems to be fastest so far on large lists. Cases[Tally@list, {_, 1}][[All, 1]] is about 10% faster on unpacked arrays, slightly slower on packed arrays. Jan 23, 2019 at 12:58