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}?
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]
Select[a, Count[a, #] == 1 &] gives you the result directly; you don't need to use ReplaceAll.
– kglr
Jan 23 '19 at 12:30
Counts is probably the best way, since you only have to traverse the whole list once.
– Sjoerd Smit
Jan 23 '19 at 12:41
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.
– Michael E2
Jan 23 '19 at 12:58
arepeated here? – Kuba Jan 23 '19 at 12:23