<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wenhan</title>
    <description>The latest articles on DEV Community by Wenhan (@wenhan129).</description>
    <link>https://dev.to/wenhan129</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F179324%2Ffb6af6d1-5278-40b5-a6f8-4494a79533d6.jpeg</url>
      <title>DEV Community: Wenhan</title>
      <link>https://dev.to/wenhan129</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wenhan129"/>
    <language>en</language>
    <item>
      <title>Thoughts about a two pointer array problem - #27 LeetCode</title>
      <dc:creator>Wenhan</dc:creator>
      <pubDate>Tue, 09 Jul 2019 16:42:54 +0000</pubDate>
      <link>https://dev.to/wenhan129/thoughts-about-a-two-pointer-array-problem-27-leetcode-26pg</link>
      <guid>https://dev.to/wenhan129/thoughts-about-a-two-pointer-array-problem-27-leetcode-26pg</guid>
      <description>&lt;p&gt;Please check the link first for the details of the problem.&lt;br&gt;
&lt;a href="https://leetcode.com/problems/remove-element/"&gt;Remove Element&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given an array &lt;code&gt;nums&lt;/code&gt; and a value &lt;code&gt;val&lt;/code&gt;, 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 &lt;strong&gt;O(1)&lt;/strong&gt; extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length.&lt;/p&gt;

&lt;p&gt;Given &lt;code&gt;nums = [3,2,2,3], val = 3&lt;/code&gt;, your function should return length = 2, with the first two elements of &lt;code&gt;nums&lt;/code&gt; being 2. It doesn't matter what you leave beyond the returned length.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first thought to this problem was a naive solution — to create another array for &lt;em&gt;filtered&lt;/em&gt; values. However, under the requirement of memory usage. We have to modify the input array in place.&lt;br&gt;
The output should be a number N which represents the &lt;em&gt;valid length&lt;/em&gt; of the array, which means the first N elements should not have the &lt;code&gt;val&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I decided to use two pointer method to solve this problem.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Why?
With two pointers &lt;code&gt;index&lt;/code&gt;, &lt;code&gt;targetIndex&lt;/code&gt;, &lt;code&gt;index&lt;/code&gt; representing the index of &lt;code&gt;nums&lt;/code&gt; loop function and &lt;code&gt;targetIndex&lt;/code&gt; representing the result array index which starts from 0. Imagine you are operating a new array based on the original array, you are just moving the qualified values forward into the new array.
In the loop of &lt;code&gt;nums&lt;/code&gt;,  if &lt;code&gt;nums[index]&lt;/code&gt; is not equal to &lt;code&gt;val&lt;/code&gt;, we move the values to the new array, &lt;code&gt;nums[targetIndex] = nums[index]&lt;/code&gt; and increment the &lt;code&gt;targetIndex&lt;/code&gt; by 1 to expand the new array. If &lt;code&gt;nums[index]&lt;/code&gt;is equal to &lt;code&gt;val&lt;/code&gt; we just skip the the value movement, so the new array will only include the qualified values.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number[]} nums
 * @param {number} val
 * @return {number}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;removeElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrLength&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;targetIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arrLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;targetIndex&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;];}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;targetIndex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
