## 27. Remove Element

Given an array and a value, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example:

```Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.
```

b'
\n\n

## Summary

\n

This is a pretty easy problem, but one may get confused by the term "in-place" and thought it is impossible to remove an element from the array without making a copy of the array.

\n

## Hints

\n
\n
1. Try two pointers.
2. \n
3. Did you use the property of "the order of elements can be changed"?
4. \n
5. What happens when the elements to remove are rare?
6. \n
\n

## Solution

\n
\n

#### Approach #1 (Two Pointers) [Accepted]

\n

Intuition

\n

Since question asked us to remove all elements of the given value in-place, we have to handle it with extra space. How to solve it? We can keep two pointers and , where is the slow-runner while is the fast-runner.

\n

Algorithm

\n

When equals to the given value, skip this element by incrementing . As long as , we copy to and increment both indexes at the same time. Repeat the process until reaches the end of the array and the new length is .

\n

This solution is very similar to the solution to Remove Duplicates from Sorted Array.

\n
`public int removeElement(int[] nums, int val) {\n    int i = 0;\n    for (int j = 0; j < nums.length; j++) {\n        if (nums[j] != val) {\n            nums[i] = nums[j];\n            i++;\n        }\n    }\n    return i;\n}\n`
\n

Complexity analysis

\n
\n
• \n

Time complexity : .\nAssume the array has a total of elements, both and traverse at most steps.

\n
• \n
• \n

Space complexity : .

\n
• \n
\n
\n

#### Approach #2 (Two Pointers - when elements to remove are rare) [Accepted]

\n

Intuition

\n

Now consider cases where the array contains few elements to remove. For example, . The previous algorithm will do unnecessary copy operation of the first four elements. Another example is . It seems unnecessary to move elements one step left as the problem description mentions that the order of elements could be changed.

\n

Algorithm

\n

When we encounter , we can swap the current element out with the last element and dispose the last one. This essentially reduces the array\'s size by 1.

\n

Note that the last element that was swapped in could be the value you want to remove itself. But don\'t worry, in the next iteration we will still check this element.

\n
`public int removeElement(int[] nums, int val) {\n    int i = 0;\n    int n = nums.length;\n    while (i < n) {\n        if (nums[i] == val) {\n            nums[i] = nums[n - 1];\n            // reduce array size by one\n            n--;\n        } else {\n            i++;\n        }\n    }\n    return n;\n}\n`
\n

Complexity analysis

\n
\n
• \n

Time complexity : .\nBoth and traverse at most steps. In this approach, the number of assignment operation is equal to the number of elements to remove. So it is more efficient if elements to remove are rare.

\n
• \n
• \n

Space complexity : .

\n
• \n
\n

Analysis written by @feelxia, revised by @1337c0d3r.

\n
'