{"id":26,"date":"2017-03-18T01:54:20","date_gmt":"2017-03-18T08:54:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=26"},"modified":"2018-04-19T08:43:44","modified_gmt":"2018-04-19T15:43:44","slug":"leetcode-422-find-all-duplicates-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-422-find-all-duplicates-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 422. Find All Duplicates in an Array"},"content":{"rendered":"<p>Given an array of integers, 1 \u2264 a[i] \u2264 <i>n<\/i> (<i>n<\/i> = size of array), some elements appear <b>twice<\/b> and others appear <b>once<\/b>.<\/p>\n<p>Find all the elements that appear <b>twice<\/b> in this array.<\/p>\n<p>Could you do it without extra space and in O(<i>n<\/i>) runtime?<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"\"><b>Input:<\/b>\r\n[4,3,2,7,8,2,3,1]\r\n\r\n<b>Output:<\/b>\r\n[2,3]<\/pre>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n    vector&lt;int&gt; findDuplicates(vector&lt;int&gt;&amp; nums) {\r\n        vector&lt;int&gt; ans;\r\n        for(auto num : nums) {\r\n            int index = abs(num)-1;\r\n            if (nums[index] &lt; 0)\r\n                ans.push_back(index+1);\r\n            nums[index]*=-1;\r\n        }\r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers, 1 \u2264 a[i] \u2264 n (n = size of array), some elements appear twice and others appear once. Find all&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[20,21,6],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-array","tag-deduplication","tag-leetcode","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/26","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=26"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/2736"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}