<?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>duplicate Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/duplicate/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/duplicate/</link>
	<description></description>
	<lastBuildDate>Tue, 01 Oct 2019 06:30:54 +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>duplicate Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/duplicate/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 80. Remove Duplicates from Sorted Array II</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-80-remove-duplicates-from-sorted-array-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-80-remove-duplicates-from-sorted-array-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 01 Oct 2019 06:30:11 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5669</guid>

					<description><![CDATA[<p>Given a sorted array&#160;nums, remove the duplicates&#160;in-place&#160;such that duplicates appeared at most&#160;twice&#160;and return the new length. Do not allocate extra space for another array, you&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-80-remove-duplicates-from-sorted-array-ii/">花花酱 LeetCode 80. Remove Duplicates from Sorted Array 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[
<p>Given a sorted array&nbsp;<em>nums</em>, remove the duplicates&nbsp;<a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noreferrer noopener"><strong>in-place</strong></a>&nbsp;such that duplicates appeared at most&nbsp;<em>twice</em>&nbsp;and return the new length.</p>



<p>Do not allocate extra space for another array, you must do this by&nbsp;<strong>modifying the input array&nbsp;<a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank" rel="noreferrer noopener">in-place</a></strong>&nbsp;with O(1) extra memory.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false">Given <em>nums</em> = <strong>[1,1,1,2,2,3]</strong>,

Your function should return length = <strong><code>5</code></strong>, with the first five elements of <em><code>nums</code></em> being <strong><code>1, 1, 2, 2</code></strong> and <strong>3</strong> respectively.

It doesn't matter what you leave beyond the returned length.</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false">Given <em>nums</em> = <strong>[0,0,1,1,1,1,2,3,3]</strong>,

Your function should return length = <strong><code>7</code></strong>, with the first seven elements of <em><code>nums</code></em> being modified to&nbsp;<strong><code>0</code></strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong> and&nbsp;<strong>3</strong> respectively.

It doesn't matter what values are set beyond&nbsp;the returned length.</pre>



<h2><strong>Solution:</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<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;= 2) return n;

    int index = 1;
    int len = 1;
    int last = nums[0];

    while (index &lt; n) {
      int count = 1;
      while (index &lt; n &amp;&amp; nums[index] == last) {
        ++count;
        ++index;
      }

      if (count &gt;= 2) nums[len++] = last;        

      if (index &lt; n) {
        last = nums[index++];
        nums[len++] = last;
      }
    }

    return len;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/list/leetcode-82-remove-duplicates-from-sorted-list-ii/">https://zxi.mytechroad.com/blog/list/leetcode-82-remove-duplicates-from-sorted-list-ii/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-80-remove-duplicates-from-sorted-array-ii/">花花酱 LeetCode 80. Remove Duplicates from Sorted Array 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/algorithms/array/leetcode-80-remove-duplicates-from-sorted-array-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 82. Remove Duplicates from Sorted List II</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-82-remove-duplicates-from-sorted-list-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-82-remove-duplicates-from-sorted-list-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 01 Oct 2019 06:14:56 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5665</guid>

					<description><![CDATA[<p>Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only&#160;distinct&#160;numbers from the original list. Example 1: Input: 1-&#62;2-&#62;3-&#62;3-&#62;4-&#62;4-&#62;5 Output: 1-&#62;2-&#62;5 Example&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-82-remove-duplicates-from-sorted-list-ii/">花花酱 LeetCode 82. Remove Duplicates from Sorted List 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[
<p>Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only&nbsp;<em>distinct</em>&nbsp;numbers from the original list.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 1-&gt;2-&gt;3-&gt;3-&gt;4-&gt;4-&gt;5
<strong>Output:</strong> 1-&gt;2-&gt;5
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 1-&gt;1-&gt;1-&gt;2-&gt;3
<strong>Output:</strong> 2-&gt;3</pre>



<h2><strong>Solution:</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode *deleteDuplicates(ListNode *head) {
    if (!head) return nullptr;
    ListNode dummy = ListNode(0);
    ListNode *p = &amp;dummy;
    while (head) {
      int c = 0;
      while (head-&gt;next &amp;&amp; head-&gt;next-&gt;val == head-&gt;val) {
        head = head-&gt;next;
        c++;
      }
      if (c == 0)
        p = p-&gt;next = head;        
      else
        p-&gt;next = nullptr;      
      head = head-&gt;next;
    }
    return dummy.next;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/list/leetcode-83-remove-duplicates-from-sorted-list/">https://zxi.mytechroad.com/blog/list/leetcode-83-remove-duplicates-from-sorted-list/</a></li><li><a href="https://zxi.mytechroad.com/blog/list/leetcode-86-partition-list/">https://zxi.mytechroad.com/blog/list/leetcode-86-partition-list/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-82-remove-duplicates-from-sorted-list-ii/">花花酱 LeetCode 82. Remove Duplicates from Sorted List 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/list/leetcode-82-remove-duplicates-from-sorted-list-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 83. Remove Duplicates from Sorted List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-83-remove-duplicates-from-sorted-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-83-remove-duplicates-from-sorted-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 30 Sep 2019 16:36:34 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[list]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5661</guid>

					<description><![CDATA[<p>Given a sorted linked list, delete all duplicates such that each element appear only&#160;once. Example 1: Input: 1-&#62;1-&#62;2 Output: 1-&#62;2 Example 2: Input: 1-&#62;1-&#62;2-&#62;3-&#62;3 Output:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-83-remove-duplicates-from-sorted-list/">花花酱 LeetCode 83. Remove Duplicates from Sorted List</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 a sorted linked list, delete all duplicates such that each element appear only&nbsp;<em>once</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 1-&gt;1-&gt;2
<strong>Output:</strong> 1-&gt;2
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 1-&gt;1-&gt;2-&gt;3-&gt;3
<strong>Output:</strong> 1-&gt;2-&gt;3</pre>



<h2><strong>Solution:</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode *deleteDuplicates(ListNode *head) {        
    if (!head) return head;
    ListNode d(0);
    ListNode* t = &amp;d;
    t-&gt;next = head;
    while (head) {
      while (head &amp;&amp; head-&gt;val == t-&gt;next-&gt;val) head = head-&gt;next;
      t-&gt;next-&gt;next = head;
      t = t-&gt;next;
    }
    return d.next;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-83-remove-duplicates-from-sorted-list/">花花酱 LeetCode 83. Remove Duplicates from Sorted List</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/list/leetcode-83-remove-duplicates-from-sorted-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 217. Contains Duplicate</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-217-contains-duplicate/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-217-contains-duplicate/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 23 Jul 2019 05:24:40 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[sorting]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5340</guid>

					<description><![CDATA[<p>Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-217-contains-duplicate/">花花酱 LeetCode 217. Contains Duplicate</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, find if the array contains any duplicates.</p>



<p>Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> [1,2,3,1]
<strong>Output:</strong> true</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong>[1,2,3,4]
<strong>Output:</strong> false</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong>[1,1,1,3,3,4,3,2,4,2]
<strong>Output:</strong> true</pre>



<h2><strong>Solution 1: HashTable</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 48ms/16.5MB
class Solution {
public:
  bool containsDuplicate(vector&lt;int&gt;&amp; nums) {
    unordered_set&lt;int&gt; s;
    for (int num : nums)
      if (!s.insert(num).second) return true;
    return false;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Sorting</strong></h2>



<p>Time complexity: O(nlogn)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua, 28ms/11.4MB
class Solution {
public:
  bool containsDuplicate(vector&lt;int&gt;&amp; nums) {
    sort(begin(nums), end(nums));
    for (int i = 1; i &lt; nums.size(); ++i)
      if (nums[i] == nums[i - 1]) return true;
    return false;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-217-contains-duplicate/">花花酱 LeetCode 217. Contains Duplicate</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/hashtable/leetcode-217-contains-duplicate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 287. Find the Duplicate Number</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 15 Jul 2018 05:05:57 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(1) space]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3159</guid>

					<description><![CDATA[<p>Problem Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/">花花酱 LeetCode 287. Find the Duplicate Number</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 an array <i>nums</i> containing <i>n</i> + 1 integers where each integer is between 1 and <i>n</i> (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.</p>
<p><b>Example 1:</b></p>
<pre class="crayon:false"><b>Input:</b> <code>[1,3,4,2,2]
</code><b>Output:</b> 2</pre>
<p><b>Example 2:</b></p>
<pre class="crayon:false "><b>Input:</b> [3,1,3,4,2]
<b>Output:</b> 3</pre>
<p><b>Note:</b></p>
<ol>
<li>You <b>must not</b> modify the array (assume the array is read only).</li>
<li>You must use only constant, <i>O</i>(1) extra space.</li>
<li>Your runtime complexity should be less than <em>O</em>(<em>n</em><sup>2</sup>).</li>
<li>There is only one duplicate number in the array, but it could be repeated more than once.</li>
</ol>
<h1><strong>Solution1: Binary Search</strong></h1>
<p>Time complexity: O(nlogn)</p>
<p>Space complexity: O(1)</p>
<p>Find the smallest m such that len(nums &lt;= m) &gt; m, which means m is the duplicate number.</p>
<p>In the sorted form [1, 2, &#8230;, m1, m2, m + 1, &#8230;, n]</p>
<p>There are m+1 numbers &lt;= m</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 8 ms
class Solution {
public:
  int findDuplicate(vector&lt;int&gt;&amp; nums) {
    int l = 1;
    int r = nums.size();
    while (l &lt; r) {
      int m = (r - l) / 2 + l;
      int count = 0; // len(nums &lt;= m)
      for (int num : nums)
        if (num &lt;= m) ++count;
      if (count &lt;= m)
        l = m + 1;
      else
        r = m;
    }
    return l;
  }
};</pre><p></p>
<h1><strong>Solution: Linked list cycle</strong></h1>
<p>Convert the problem to find the entry point of the cycle in a linked list.</p>
<p>Take the number in the array as the index of next node.</p>
<p>[1,3,4,2,2]</p>
<p>0-&gt;1-&gt;3-&gt;2-&gt;4-&gt;2 cycle: 2-&gt;4-&gt;2</p>
<p>[3,1,3,4,2]</p>
<p>0-&gt;3-&gt;4-&gt;2-&gt;3-&gt;4-&gt;2 cycle 3-&gt;4-&gt;2-&gt;3</p>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int findDuplicate(vector&lt;int&gt;&amp; nums) {
    int slow = 0;
    int fast = 0;
    while (true) {
      slow = nums[slow];
      fast = nums[nums[fast]];
      if (slow == fast) break;
    }
    fast = 0;
    while (fast != slow) {
      slow = nums[slow];
      fast = nums[fast];
    }
    return slow;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/binary-search/leetcode-287-find-the-duplicate-number/">花花酱 LeetCode 287. Find the Duplicate Number</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/binary-search/leetcode-287-find-the-duplicate-number/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
