I have a list, say {10,9,8,7,2,3,5,4,3,2,1}
What I want to achieve is to delete the elements that are not in decreasing order recursively. Namely, the output should be {10,9,8,7,5,4,3,2,1}
, which deletes {2,3}
in the list.
I am seeking some elegant and efficient way to do so. Naively, I have thought about these methods:
Using a loop and starting from the first element, compare the current element with the next element. Remove the current if
current < next
, and move the pointer to the previous element. Do it recursively until the next is smaller than the current. Clearly, this is a procedural programming which is not preferable in Mathematica.Using
Nest[]
. First doDifference[list]
and remove all the positive elements. Update list and do it recursively until theDifference[list]
are all negative.Using
Nest[]
and find the minimal peaks in the list, delete the peak step by step.
All these methods use loops or Nest
and hence are not very efficient. I am wondering if there is a built-in function that can do this efficiently.
FixedPoint[Delete[#, Position[Differences[#], _?Positive]] &, #] &
? $\endgroup${10, 9, 11, 12, 8, 7, 2, 3, 5, 4, 3, 2, 1}
to be? $\endgroup$