> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/27.-remove-element.md).

# 27. Remove Element

## 1.問題

![](/files/-LMD-bRms7sOtK0sbA9Q)

## 2.想法

* 提問
* function header, parameter
* test input
* 說明想法
  * 當碰到相同字母時,  讓計數器增加
  * 當不同字母時, 索引值就是減去計數器
* 測試計算複雜度: O(n)

## **3.程式碼**

```
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int size = nums.size();
        if (size == 0) {
            return 0;
        }
        int index = 0;
        for (int i = 0; i < size; i++) {
            if (nums[i] != val) {
                nums[index++] = nums[i];
            }
        }
        return index;
    }
};
```
