The Remove Element solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Remove Element
C++, Python and TypeScript solutions for Remove Element.
Solution files
Python•remove-element/synced-solution.py
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
k = 0
for n in nums:
if n != val:
nums[k] = n; k += 1
return k
C++•remove-element/synced-solution.cpp
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int k = 0;
for (int n : nums) if (n != val) nums[k++] = n;
return k;
}
};
TypeScript•remove-element/synced-solution.ts
function removeElement(nums: number[], val: number): number {
let k = 0;
for (const n of nums) if (n !== val) nums[k++] = n;
return k;
}