Problem
Problem page: https://leetcode.com/problems/remove-element/
Topics: Array, Two Pointers
Difficulty: Easy
Similar problems: (E) Remove Linked List Elements, (E) Move Zeroes
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Analysis
The problem hints that I can move the elements I need to remove to the end of the array, so I use the indexes m and n. I use m to scan the array from left to right looking for the target value, while n works from right to left to track a position at the end that I can swap with.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-remove-element/
