---
# System prepended metadata

title: 0027. Remove Element
tags: [Leetcode, 双指针]

---

# 0027. Remove Element
###### tags: `Leetcode` `双指针`
Link: https://leetcode.com/problems/remove-element/
历史性的辉煌时刻，十分钟解一题
## Code
```c
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int i = 0;
        for(int j = 0;j < nums.size();j++){
            if(nums[j]!=val){
                nums[i] = nums[j];
                i++;
            }
        }
        return i;
    }
};
```
## Code in Java
# 历史的黑暗时刻，一小时没做出来
```java
class Solution {
    public int removeElement(int[] nums, int val) {

        // if (nums.length == 0) return 0;
        int j = 0;
        for (int i = 0; i < nums.length; i++){
            if (nums[i] != val){
                if (i > j) nums[j] = nums[i];
                j++;
            }
        }
        return j;
    }
}
```