{"id":108,"date":"2017-07-28T19:18:01","date_gmt":"2017-07-28T11:18:01","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=108"},"modified":"2018-11-27T16:09:40","modified_gmt":"2018-11-27T08:09:40","slug":"27-remove-element","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=108","title":{"rendered":"27. Remove Element"},"content":{"rendered":"<p>Given an array\u00a0<em>nums<\/em>\u00a0and a value\u00a0<em>val<\/em>, remove all instances of that value\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noopener\"><strong>in-place<\/strong><\/a>\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>The order of elements can be changed. It doesn&#8217;t matter what you leave beyond the new length.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre>Given <em>nums<\/em> = <strong>[3,2,2,3]<\/strong>, <em>val<\/em> = <strong>3<\/strong>,\r\n\r\nYour function should return length = <strong>2<\/strong>, with the first two elements of <em>nums<\/em> being <strong>2<\/strong>.\r\n\r\nIt doesn't matter what you leave beyond the returned length.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre>Given <em>nums<\/em> = <strong>[0,1,2,2,3,0,4,2]<\/strong>, <em>val<\/em> = <strong>2<\/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> containing\u00a0<strong><code>0<\/code><\/strong>, <strong><code>1<\/code><\/strong>, <strong><code>3<\/code><\/strong>, <strong><code>0<\/code><\/strong>, and\u00a0<strong>4<\/strong>. Note that the order of those five elements can be arbitrary. 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 = removeElement(nums, val);\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 removeElement(self, nums, val):\r\n        &quot;&quot;&quot;\r\n        :type nums: List[int]\r\n        :type val: int\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        for i in range(0, len(nums)):\r\n            try:\r\n                nums.remove(val)\r\n            except Exception as e:\r\n                break\r\n        return len(nums)\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given an array\u00a0nums\u00a0and a value\u00a0val, remove all instanc<a href=\"https:\/\/www.ccagml.com\/?p=108\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">27. Remove Element<\/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\/108"}],"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=108"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/108\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}