# [27\. Remove Element](https://leetcode.com/problems/remove-element/)
:::spoiler Hint
```cpp=
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
}
};
```
:::
:::spoiler Solution
```cpp=
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;
}
};
```
- 時間複雜度:$O(N)$
- 空間複雜度:$O(1)$
:::