{"id":105,"date":"2017-07-28T19:06:16","date_gmt":"2017-07-28T11:06:16","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=105"},"modified":"2018-11-27T16:09:49","modified_gmt":"2018-11-27T08:09:49","slug":"26-remove-duplicates-from-sorted-array","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=105","title":{"rendered":"26. Remove Duplicates from Sorted Array"},"content":{"rendered":"<p>Given a sorted array\u00a0<em>nums<\/em>, remove the duplicates\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noopener\"><strong>in-place<\/strong><\/a>\u00a0such that each element appear only\u00a0<em>once<\/em>\u00a0and return the new length.<\/p>\n<p>Do not allocate extra space for another array, you must do this by\u00a0<strong>modifying the input array\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noopener\">in-place<\/a><\/strong>\u00a0with O(1) extra memory.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre>Given <em>nums<\/em> = <strong>[1,1,2]<\/strong>,\r\n\r\nYour 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>\n<p><strong>Example 2:<\/strong><\/p>\n<pre>Given <em>nums<\/em> = <strong>[0,0,1,1,1,2,2,3,3,4]<\/strong>,\r\n\r\nYour function should return length = <strong><code>5<\/code><\/strong>, with the first five elements of <em><code>nums<\/code><\/em> being modified to\u00a0<strong><code>0<\/code><\/strong>, <strong><code>1<\/code><\/strong>, <strong><code>2<\/code><\/strong>, <strong><code>3<\/code><\/strong>, and\u00a0<strong><code>4<\/code><\/strong> respectively. It doesn't matter what values are set beyond\u00a0the returned length.<\/pre>\n<p><strong>Clarification:<\/strong><\/p>\n<p>Confused why the returned value is an integer but your answer is an array?<\/p>\n<p>Note that the input array is passed in by\u00a0<strong>reference<\/strong>, which means modification to the input array will be known to the caller as well.<\/p>\n<p>Internally you can think of this:<\/p>\n<pre>\/\/ <strong>nums<\/strong> is passed in by reference. (i.e., without making a copy)\r\nint len = removeDuplicates(nums);\r\n\r\n\/\/ any modification to <strong>nums<\/strong> in your function would be known by the caller.\r\n\/\/ using the length returned by your function, it prints the first <strong>len<\/strong> elements.\r\nfor (int i = 0; i &lt; len; i++) {\r\n\u00a0 \u00a0 print(nums[i]);\r\n}<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def removeDuplicates(self, nums):\r\n        &quot;&quot;&quot;\r\n        :type nums: List[int]\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        if not nums:\r\n            return 0\r\n        a = 0\r\n        for i in nums[1::]:\r\n            if i != nums[a]:\r\n                a += 1\r\n                nums[a] = i\r\n        return a + 1\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a sorted array\u00a0nums, remove the duplicates\u00a0in-pla<a href=\"https:\/\/www.ccagml.com\/?p=105\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">26. Remove Duplicates from Sorted Array<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/105"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}