0
\$\begingroup\$

If you are given an array of numbers between 1 and 100 and an integer n, you must remove any instances of an item that appear greater than n times.

For example:

In : [92, 41, 81, 47, 28, 92, 84, 92, 81], n = 1
Out: [92, 41, 81, 47, 28, 84]

The most aesthetic/simple algorithm wins. And since this is a , you get to decide!

\$\endgroup\$
2
  • 4
    \$\begingroup\$ You should know that popularity-contest is effectively deprecated at this point, at least in the form of "nicest answer wins". Moreover, I think this task is straightforward enough that in most languages the normal idiomatic way to do this is probably the simplest, and anything else would just be adornment for the sake of it. \$\endgroup\$
    – xnor
    May 14, 2020 at 21:58
  • 5
    \$\begingroup\$ In the future, consider using the Sandbox to propose challenges before posting them \$\endgroup\$ May 14, 2020 at 22:28

1 Answer 1

1
\$\begingroup\$

J, 21 16 15 bytes

(>:+/@(={:)\)#]

Try it online!

Overall, we make a boolean mask and apply it to the original list: (...)#]

To construct the mask, for each prefix \ we sum +/@ the number of times an element equals the last element of that prefix (={:), which gives us a list the same size as the original, where each element holds the number of times the element has appeared so far. This is:

1 1 1 1 1 2 1 3 2

for the example list.

Then we check which elements are less than or equal >: to the left arg:

1 1 1 1 1 0 1 0 0

producing the mask we want.

\$\endgroup\$

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