<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>deduplication Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/deduplication/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/deduplication/</link>
	<description></description>
	<lastBuildDate>Tue, 02 Oct 2018 20:17:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>deduplication Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/deduplication/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 26. Remove Duplicates from Sorted Array</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Oct 2018 16:33:53 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[deduplication]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4130</guid>

					<description><![CDATA[<p>Problem Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array,&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/">花花酱 LeetCode 26. Remove Duplicates from Sorted Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given a sorted array <em>nums</em>, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noopener"><strong>in-place</strong></a> such that each element appear only <em>once</em> and return the new length.</p>
<p>Do not allocate extra space for another array, you must do this by <strong>modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noopener">in-place</a></strong> with O(1) extra memory.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false">Given <em>nums</em> = <strong>[1,1,2]</strong>,

Your function should return length = <strong><code>2</code></strong>, with the first two elements of <em><code>nums</code></em> being <strong><code>1</code></strong> and <strong><code>2</code></strong> respectively. It doesn't matter what you leave beyond the returned length.</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false">Given <em>nums</em> = <strong>[0,0,1,1,1,2,2,3,3,4]</strong>,

Your function should return length = <strong><code>5</code></strong>, with the first five elements of <em><code>nums</code></em> being modified to <strong><code>0</code></strong>, <strong><code>1</code></strong>, <strong><code>2</code></strong>, <strong><code>3</code></strong>, and <strong><code>4</code></strong> respectively. It doesn't matter what values are set beyond the returned length.</pre>
<p><strong>Clarification:</strong></p>
<p>Confused why the returned value is an integer but your answer is an array?</p>
<p>Note that the input array is passed in by <strong>reference</strong>, which means modification to the input array will be known to the caller as well.</p>
<p>Internally you can think of this:</p>
<pre class="crayon:false">// <strong>nums</strong> is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to <strong>nums</strong> in your function would be known by the caller.
// using the length returned by your function, it prints the first <strong>len</strong> elements.
for (int i = 0; i &lt; len; i++) {
    print(nums[i]);
}</pre>
<h1>Solution:</h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int removeDuplicates(vector&lt;int&gt;&amp; nums) {
    const int n = nums.size();
    if (n &lt;= 1) return n;        
    int ans = 0;
    int i = 0;
    while (i &lt; n) {
      nums[ans++] = nums[i];
      int j = i + 1;
      while (j &lt; n &amp;&amp; nums[j] == nums[j - 1]) ++j;
      i = j;
    }
    return ans;
  }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/">花花酱 LeetCode 26. Remove Duplicates from Sorted Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-26-remove-duplicates-from-sorted-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 47. Permutations II</title>
		<link>https://zxi.mytechroad.com/blog/searching/leetcode-47-permutations-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/searching/leetcode-47-permutations-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 26 Jul 2018 15:17:47 +0000</pubDate>
				<category><![CDATA[Search]]></category>
		<category><![CDATA[deduplication]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[permutation]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3307</guid>

					<description><![CDATA[<p>Problem Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] Solution&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-47-permutations-ii/">花花酱 LeetCode 47. Permutations II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem</h1>
<p>Given a collection of numbers that might contain duplicates, return all possible unique permutations.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> [1,1,2]
<strong>Output:</strong>
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]</pre>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n!)</p>
<p>Space complexity: O(n + k)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 20 ms
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; permuteUnique(vector&lt;int&gt;&amp; nums) {
    sort(begin(nums), end(nums));
    vector&lt;vector&lt;int&gt;&gt; ans;
    vector&lt;int&gt; used(nums.size());
    vector&lt;int&gt; cur;
    dfs(nums, cur, used, ans);
    return ans;
  }
private:
  void dfs(const vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; cur, vector&lt;int&gt;&amp; used, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {
    if (cur.size() == nums.size()) {
      ans.push_back(cur);
      return;
    }
    for (int i = 0; i &lt; nums.size(); ++i) {
      if (used[i]) continue;
      // Same number can be only used once at each depth.
      if (i &gt; 0 &amp;&amp; nums[i] == nums[i - 1] &amp;&amp; !used[i - 1]) continue;
      used[i] = 1;
      cur.push_back(nums[i]);
      dfs(nums, cur, used, ans);
      cur.pop_back();
      used[i] = 0;
    }
  }
};</pre><p></p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-784-letter-case-permutation/">花花酱 LeetCode 784. Letter Case Permutation</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/searching/leetcode-47-permutations-ii/">花花酱 LeetCode 47. Permutations II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/searching/leetcode-47-permutations-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 422. Find All Duplicates in an Array</title>
		<link>https://zxi.mytechroad.com/blog/leetcode/leetcode-422-find-all-duplicates-in-an-array/</link>
					<comments>https://zxi.mytechroad.com/blog/leetcode/leetcode-422-find-all-duplicates-in-an-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 18 Mar 2017 08:54:20 +0000</pubDate>
				<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[deduplication]]></category>
		<category><![CDATA[leetcode]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=26</guid>

					<description><![CDATA[<p>Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-422-find-all-duplicates-in-an-array/">花花酱 LeetCode 422. Find All Duplicates in an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Given an array of integers, 1 ≤ a[i] ≤ <i>n</i> (<i>n</i> = size of array), some elements appear <b>twice</b> and others appear <b>once</b>.</p>
<p>Find all the elements that appear <b>twice</b> in this array.</p>
<p>Could you do it without extra space and in O(<i>n</i>) runtime?</p>
<p><b>Example:</b></p><pre class="crayon-plain-tag">&lt;b&gt;Input:&lt;/b&gt;
[4,3,2,7,8,2,3,1]

&lt;b&gt;Output:&lt;/b&gt;
[2,3]</pre><p></p><pre class="crayon-plain-tag">class Solution {
public:
    vector&lt;int&gt; findDuplicates(vector&lt;int&gt;&amp; nums) {
        vector&lt;int&gt; ans;
        for(auto num : nums) {
            int index = abs(num)-1;
            if (nums[index] &lt; 0)
                ans.push_back(index+1);
            nums[index]*=-1;
        }
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/leetcode/leetcode-422-find-all-duplicates-in-an-array/">花花酱 LeetCode 422. Find All Duplicates in an Array</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/leetcode/leetcode-422-find-all-duplicates-in-an-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
