As you have discovered, .filter()
iterates the entire array, testing each value against your filter function. That is not the task you have articulated.
The key to removing a single element is .splice()
. Your task is simply to tell splice which item(s) to remove:
a.splice(a.indexOf( k ), 1);
Alternatively, you could remove the last index with:
a.splice(a.lastIndexOf( k ), 1);
In "human", arr.splice(k, n)
reads "Starting at index k, remove the next n elements."
Of course, if you don't mind the overhead of sorting (or changing the item order!), then you can do something like:
a.sort().pop();
Which will sort the array, and then remove the final element -- which is also the largest.