> For the complete documentation index, see [llms.txt](https://cai-sen-se.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cai-sen-se.gitbook.io/leetcode/1-200/27.-yi-chu-yuan-su.md).

# 27. 移除元素

<https://leetcode-cn.com/problems/remove-element/>

## 解法一：

用i标记无val区的尾部，即`[0:i]`是无val的区域；j一次遍历数组，遇到不等于val的就赋给i处，然后i后移

```python
class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        i = j = 0
        while j < len(nums):
            if nums[j] != val:
                nums[i] = nums[j]    #无val区扩张
                i += 1    
            j += 1
        return i    #i即为无val区的长度
```
