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
.
$\endgroup$
Counts
is probably the best way, since you only have to traverse the whole list once.
$\endgroup$
Jan 23, 2019 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.
$\endgroup$
Jan 23, 2019 at 12:58
a
repeated here? $\endgroup$