Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. list = {a, a, a, b, c, a, b, b, b, e};
  2.  
  3. elementsplus4 = Select[list, Count[list, #] == 4 &];
  4.  
  5. {a, a, a, b, a, b, b, b}
  6.  
  7. Cases[Tally[list], {x_, 4} :> x]
  8.  
  9. Cases[Tally @ list, {x_, 4} :> x]
  10.  
  11. Cases[Split @ Sort @ list, {x_, _, _, _} :> x]
  12.  
  13. Cases[Split @ Sort @ list, {Repeated[x_, {4}]} :> x]
  14.  
  15. Cases[Last @ Reap[Sow[1, list], _, {#, Tr@#2} &], {x_, 4} :> x]
  16.  
  17. Module[{c},
  18. c[_] = 0;
  19. Scan[c[#]++ &, list];
  20. Cases[DownValues[c], (_@_@x_ :> 4) :> x]
  21. ]
  22.  
  23. list = FromCharacterCode /@ RandomInteger[15000, 100000];
  24.  
  25. Cases[Tally @ list, {x_, 4} :> x] // Timing // First
  26.  
  27. Cases[Last @ Reap[Sow[1, list], _, {#, Tr@#2} &], {x_, 4} :> x] // Timing // First
  28.  
  29. Module[{c},
  30. c[_] = 0;
  31. Scan[c[#]++ &, list];
  32. Cases[DownValues[c], (_@_@x_ :> 4) :> x]
  33. ] // Timing // First
  34.  
  35. 0.546
  36.  
  37. 0.109
  38.  
  39. 0.2622
  40.  
  41. stringTally = Last @ Reap[Sow[1, #], _, {#, Tr@#2} &] &;
  42.  
  43. Cases[stringTally @ list, {x_, 4} :> x] // Timing // First
  44.  
  45. 0.103
  46.  
  47. stringTally = Last @ Reap[Sow[1, #], _, {#, Tr@#2} &] &;
  48.  
  49. Cases[stringTally @ list, {x_, n_} /; n >= 4 :> x]
  50.  
  51. Cases[Split @ Sort @ list, {x_, _, _, __} :> x]
  52.  
  53. Cases[Split @ Sort @ list, {Repeated[x_, {4, ∞}]} :> x]
  54.  
  55. Cases[Last @ Reap[Sow[1, list], _, {#, Tr@#2} &], {x_, n_} /; n >= 4 :> x]
  56.  
  57. Module[{c},
  58. c[_] = 0;
  59. Scan[c[#]++ &, list];
  60. Cases[DownValues[c], (_@_@x_ :> n_) /; n >= 4 :> x]
  61. ]
  62.  
  63. DeleteDuplicates[Select[list, Count[list, #] == 4 &]]
Add Comment
Please, Sign In to add comment