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

# 27. Remove Element

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

```java
/*
    t: O(n), s: O(1)
*/
class Solution {
    public int removeElement(int[] nums, int val) {
        // because this problem only need to know the length of array after removing element
        // so use a index to record the left element
        
        int begin = 0;
        for (int num : nums) {
            if (num != val) {
                nums[begin++] = num;
            }
        }
        return begin;
    }
}
```
