# 27. Remove Element

## 1.問題

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LMCzZgqoTaf26RYhgYu%2F-LMD-bRms7sOtK0sbA9Q%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-09-12%20%E4%B8%8B%E5%8D%889.39.31.png?alt=media\&token=8b0c1c26-551c-4b89-8a8e-598eaca3bc16)

## 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;
    }
};
```
