Link: https://leetcode.com/problems/remove-element/
Solution:
Topics: in-place
Intuition
Honestly, this quite tricky to come up with if you are committed to a 1-pass solution. I kept hitting edge cases in 1 pass so I threw in the towel and just ran a second pass to count the elements that were not equal to val
.
1-pass solution is kind of subtle but it does make sense. I’m tagging this as hard because of the 1-pass edge cases (two pass is trivial).
Implementation