{"id":4533,"date":"2018-12-23T00:41:29","date_gmt":"2018-12-23T08:41:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4533"},"modified":"2018-12-23T00:42:02","modified_gmt":"2018-12-23T08:42:02","slug":"leetcode-961-n-repeated-element-in-size-2n-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/randomization\/leetcode-961-n-repeated-element-in-size-2n-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 961. N-Repeated Element in Size 2N Array"},"content":{"rendered":"\n<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>\n\n\n\n<p>Return the element repeated&nbsp;<code>N<\/code>&nbsp;times.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[1,2,3,3]<br><strong>Output: <\/strong>3<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[2,1,2,5,3,2]<br><strong>Output: <\/strong>2<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[5,1,5,2,5,3,5,4]<br><strong>Output: <\/strong>5<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><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>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution: Randomization<\/strong><\/h1>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(1) expected 4<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 72 ms\nclass Solution {\npublic:\n  int repeatedNTimes(vector<int>& A) {\n    int i = 0;\n    int j = 0;\n    while (i == j || A[i] != A[j]) {\n      i = rand() % A.size();\n      j = rand() % A.size();\n    }\n    return A[i];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a array&nbsp;A&nbsp;of size&nbsp;2N, there are&nbsp;N+1&nbsp;unique elements, and exactly one of these elements is repeated N times. Return the element repeated&nbsp;N&nbsp;times. Example 1: Input: [1,2,3,3]Output:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[446],"tags":[222,91,445],"class_list":["post-4533","post","type-post","status-publish","format-standard","hentry","category-randomization","tag-easy","tag-random","tag-repeat","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4533"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4533\/revisions"}],"predecessor-version":[{"id":4535,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4533\/revisions\/4535"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}