<?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>Randomization Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/randomization/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/randomization/</link>
	<description></description>
	<lastBuildDate>Sun, 23 Dec 2018 08:42:02 +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>Randomization Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/randomization/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 961. N-Repeated Element in Size 2N Array</title>
		<link>https://zxi.mytechroad.com/blog/randomization/leetcode-961-n-repeated-element-in-size-2n-array/</link>
					<comments>https://zxi.mytechroad.com/blog/randomization/leetcode-961-n-repeated-element-in-size-2n-array/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 23 Dec 2018 08:41:29 +0000</pubDate>
				<category><![CDATA[Randomization]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[repeat]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4533</guid>

					<description><![CDATA[<p>In a array&#160;A&#160;of size&#160;2N, there are&#160;N+1&#160;unique elements, and exactly one of these elements is repeated N times. Return the element repeated&#160;N&#160;times. Example 1: Input: [1,2,3,3]Output:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/randomization/leetcode-961-n-repeated-element-in-size-2n-array/">花花酱 LeetCode 961. N-Repeated Element in Size 2N 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>In a array&nbsp;<code>A</code>&nbsp;of size&nbsp;<code>2N</code>, there are&nbsp;<code>N+1</code>&nbsp;unique elements, and exactly one of these elements is repeated N times.</p>



<p>Return the element repeated&nbsp;<code>N</code>&nbsp;times.</p>



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



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



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



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



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



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



<p><strong>Note:</strong></p>



<ol><li><code>4 &lt;= A.length &lt;= 10000</code></li><li><code>0 &lt;= A[i] &lt; 10000</code></li><li><code>A.length</code>&nbsp;is even</li></ol>



<h1><strong>Solution: Randomization</strong></h1>



<p>Randomly pick two numbers in the array, if they are the same (25% probability) return the number, do it until the two numbers are the same.</p>



<p>Time complexity: O(1) expected 4<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, running time: 72 ms
class Solution {
public:
  int repeatedNTimes(vector&lt;int&gt;&amp; A) {
    int i = 0;
    int j = 0;
    while (i == j || A[i] != A[j]) {
      i = rand() % A.size();
      j = rand() % A.size();
    }
    return A[i];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/randomization/leetcode-961-n-repeated-element-in-size-2n-array/">花花酱 LeetCode 961. N-Repeated Element in Size 2N 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/randomization/leetcode-961-n-repeated-element-in-size-2n-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
