## Given problem

Given an array nums and a value val, 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.

For example:

``````Example 1:
Given nums = [3,2,2,3], val = 3,

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

It doesn't matter what you leave beyond the returned length.

Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.
``````

## Using two-pointers technique

In this problem, we will use two pointers:

• Supposed that slow pointer will point to another array.
• Fast pointer will iterate the current array.
• Each time we find that an element at fast pointer is different than val, we will update the value at the slow pointer.
• Otherwise, increment the fast pointer.
``````public int removeElement(int[] nums, int val) {
int slow = -1;
int fast = 0;

while (fast < nums.length ){
if (nums[fast] != val) {
++slow;
nums[slow] = nums[fast];
}

++fast;
}

return slow + 1;
}
``````

The benefits of this solution:

• Maintain the order of elements after shifting them.
• Shifting elements with in-place way.

The drawbacks of this solution:

• If an array does not contains any elements that is equal to val, it makes assignment operations redundancy.

The complexity of this solution:

• Time complexity: O(n)
• Space complexity: O(1)

## Wrapping up

• Understanding some ways of the two-pointers technique that will be applied into an array.

Refer:

https://leetcode.com/problems/remove-element/
{"ft_lang_label":"__label__en","ft_lang_prob":0.80829406,"math_prob":0.99437994,"size":1841,"snap":"2022-27-2022-33","text_gpt3_token_len":454,"char_repetition_ratio":0.12520413,"word_repetition_ratio":0.025,"special_character_ratio":0.26018468,"punctuation_ratio":0.16230367,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9804917,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-02T04:24:11Z\",\"WARC-Record-ID\":\"<urn:uuid:cf4b8cb9-1abb-48fd-853c-9ad519c90ec9>\",\"Content-Length\":\"17886\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e336dd1-50b6-43bf-9616-0becf81b82ae>\",\"WARC-Concurrent-To\":\"<urn:uuid:5ad33dfc-7701-4057-9ed4-f697df144905>\",\"WARC-IP-Address\":\"185.199.109.153\",\"WARC-Target-URI\":\"https://ducmanhphan.github.io/2020-06-01-leetcode-27-remove-multiple-elements-with-the-given-value/\",\"WARC-Payload-Digest\":\"sha1:YLKDMCIWQCTQR7MIYNPJJCWKYWVX6FIY\",\"WARC-Block-Digest\":\"sha1:EVJBGULT4BRMOX6HXW7ZVWKMYPS7MCOH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103984681.57_warc_CC-MAIN-20220702040603-20220702070603-00574.warc.gz\"}"}