27. Remove Element
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 02, 2024
Last updated : July 10, 2024
Related Topics : Array, Two Pointers
Acceptance Rate : 58.93 %
class Solution {
public int removeElement(int[] nums, int val) {
int pointer = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[pointer] = nums[i];
pointer += 1;
}
}
return pointer;
}
}
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
currentPointer = 0
for i in range(len(nums)) :
if not nums[i] == val :
nums[currentPointer] = nums[i]
currentPointer += 1
return currentPointer