3 ms·
Isn't the simple solution for doing it in place "swap reject element position with current end of array, decrement size of array?" What am i missing?
by DannyBee 10y ago
Isn't the simple solution for doing it in place "swap reject element position with current end of array, decrement size of array?"
What am i missing?
- rtpg 10y agomaybe there's a finalization step that's necessary to free up memory? This seems like the most straightforward solution though
- perfmode 10y agoWouldn't that change the order of elements?
- DannyBee 10y agoThe only guarantee i saw was that the output array did not contain the rejected elements. (and looking at the reference docs, that's all it says) It appears order is guaranteed to be maintained but it's not specified :)
- danfang 10y agoNo. [1,2,3].reject! { |x| x == 1 } would give you [3,2], rearranging the order of elements. It would work for sets.