27. Remove Element
Hint
class Solution {
public:
int removeElement(vector<int>& nums, int val)
{
// Initialize a counter to keep track of the new length of the array
// Iterate through each element of the array
for ()
{
// If the current element is not equal to the value to be removed
if ()
{
// Assign the current element to the position indicated by cnt and increment cnt
}
}
// Return the new length of the array, which is the count of elements not equal to val
}
};
class Solution {
public:
int removeElement(vector<int>& nums, int val)
{
int cnt = 0;
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != val)
{
nums[cnt++] = nums[i];
}
}
return cnt;
}
};
- T:
- S: