Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list = {a, a, a, b, c, a, b, b, b, e};
- elementsplus4 = Select[list, Count[list, #] == 4 &];
- {a, a, a, b, a, b, b, b}
- Cases[Tally[list], {x_, 4} :> x]
- Cases[Tally @ list, {x_, 4} :> x]
- Cases[Split @ Sort @ list, {x_, _, _, _} :> x]
- Cases[Split @ Sort @ list, {Repeated[x_, {4}]} :> x]
- Cases[Last @ Reap[Sow[1, list], _, {#, Tr@#2} &], {x_, 4} :> x]
- Module[{c},
- c[_] = 0;
- Scan[c[#]++ &, list];
- Cases[DownValues[c], (_@_@x_ :> 4) :> x]
- ]
- list = FromCharacterCode /@ RandomInteger[15000, 100000];
- Cases[Tally @ list, {x_, 4} :> x] // Timing // First
- Cases[Last @ Reap[Sow[1, list], _, {#, Tr@#2} &], {x_, 4} :> x] // Timing // First
- Module[{c},
- c[_] = 0;
- Scan[c[#]++ &, list];
- Cases[DownValues[c], (_@_@x_ :> 4) :> x]
- ] // Timing // First
- 0.546
- 0.109
- 0.2622
- stringTally = Last @ Reap[Sow[1, #], _, {#, Tr@#2} &] &;
- Cases[stringTally @ list, {x_, 4} :> x] // Timing // First
- 0.103
- stringTally = Last @ Reap[Sow[1, #], _, {#, Tr@#2} &] &;
- Cases[stringTally @ list, {x_, n_} /; n >= 4 :> x]
- Cases[Split @ Sort @ list, {x_, _, _, __} :> x]
- Cases[Split @ Sort @ list, {Repeated[x_, {4, ∞}]} :> x]
- Cases[Last @ Reap[Sow[1, list], _, {#, Tr@#2} &], {x_, n_} /; n >= 4 :> x]
- Module[{c},
- c[_] = 0;
- Scan[c[#]++ &, list];
- Cases[DownValues[c], (_@_@x_ :> n_) /; n >= 4 :> x]
- ]
- DeleteDuplicates[Select[list, Count[list, #] == 4 &]]
Add Comment
Please, Sign In to add comment