11
$\begingroup$

How to remove duplicate elements? I want to delete all the same elements in a list. For example, list {1,2,1,2,3,4,5,1}, I want to get {3,4,5}.

And using DeleteDuplicates function can not achieve the desired purpose: DeleteDuplicates[{1, 2, 1, 2, 3, 4, 5, 1}]

I know this method at present: {1, 2, 1, 2, 3, 4, 5, 1} // Tally // Cases [{x_, 1}:> x], but I also want to know more ingenious methods.

| improve this question | | | | |
$\endgroup$
  • 5
    $\begingroup$ I think your Tally/*Cases method is pretty cool! $\endgroup$ – Roman Mar 1 at 9:27
  • 1
    $\begingroup$ Does the order matter? $\endgroup$ – yarchik Mar 1 at 9:47
  • 2
    $\begingroup$ related $\endgroup$ – user1066 Mar 1 at 15:07
  • 3
    $\begingroup$ for fun: {1, 2, 1, 2, 3, 4, 5, 1} //. {a___, b_, c___, b_, d___} :> DeleteCases[{a, c, d}, b]. $\endgroup$ – AccidentalFourierTransform Mar 1 at 21:05
  • 1
    $\begingroup$ @user1066 Thanks for linking that. This appears to be a duplicate and I am marking it as such. If I have missed an important distinction please let me know and I shall reopen. $\endgroup$ – Mr.Wizard Mar 2 at 9:19
17
$\begingroup$
list = {1, 2, 1, 2, 3, 4, 5, 1};

Keys@Select[Counts[list], # == 1 &]
(* {3, 4, 5} *)
| improve this answer | | | | |
$\endgroup$
  • 1
    $\begingroup$ Or more functional perhaps: Keys@Select[Counts[list], EqualTo[1]] $\endgroup$ – SHuisman Mar 1 at 10:20
  • 4
    $\begingroup$ @SHuisman If you want to go operator, go all the way :-) Keys@Select[EqualTo[1]]@Counts[list] $\endgroup$ – Szabolcs Mar 1 at 10:52
  • 4
    $\begingroup$ Keys@*Select[EqualTo[1]]@*Counts is even more operator. $\endgroup$ – Roman Mar 1 at 13:15
  • 1
    $\begingroup$ I like that this will preserve order. Several of the methods shown below do not. For example, switch the position of 3 and 4 and re-run the methods below. Instead of {4,3,5}, they return {3,4,5}. The method from OP also preserves the order. $\endgroup$ – Mark R Mar 1 at 19:06
  • $\begingroup$ @Roman We're approaching Haskell levels of conciseness :-) $\endgroup$ – user76284 Mar 1 at 23:50
8
$\begingroup$
With[{t = Tally[#]}, Pick[t[[All, 1]], t[[All, 2]], 1]] &

Should outperform, hardly "ingenious "...

| improve this answer | | | | |
$\endgroup$
7
$\begingroup$

Without pattern matching:

Sort[{1, 2, 1, 2, 3, 4, 5, 1}] // 
  Split // 
  Select[Length[#] == 1 &] //
  Flatten[#, 1] &
| improve this answer | | | | |
$\endgroup$
7
$\begingroup$

For variety:

lst = {1, 2, 1, 2, 3, 4, 5, 1};

lst //. {OrderlessPatternSequence[Repeated[x_, {2, ∞}], a___]} :> {a}

{3, 4, 5}

| improve this answer | | | | |
$\endgroup$
4
$\begingroup$

A Rube Goldberg solution:

☺1 /: {♯♯___, ☺1 @ ♯_, ♯♯♯___} := {♯♯, ♯♯♯} /. ♯ | ☺1 @ ♯ -> (## &[])
☺2 = (☺3 @ ♯_ := (☺3 @ ♯ = ☺1 @ ♯; ♯); ☺3 /@ #) &;

☺2 @ {1, 2, 1, 2, 3, 4, 5, 1}

{3, 4, 5}

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ I LOL'd. +1 internet cookie for you. $\endgroup$ – ciao Mar 2 at 7:56

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