{"id":6156,"date":"2025-05-29T18:18:36","date_gmt":"2025-05-29T12:48:36","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6156"},"modified":"2025-06-05T18:33:00","modified_gmt":"2025-06-05T13:03:00","slug":"remove-element","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/remove-element\/","title":{"rendered":"Remove Element"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242B33 !important;\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n\n  .wp_blog_explanation h1,\n  .wp_blog_explanation h2,\n  .wp_blog_explanation h3,\n  .wp_blog_explanation h4,\n  .wp_blog_explanation h5,\n  .wp_blog_explanation h6,\n  .wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n  }\n<\/style>\n\n<!-- \u2705 Blog Explanation -->\n<div class=\"wp_blog_explanation\">\n  <p>\n    Given an integer array <code>nums<\/code> and an integer <code>val<\/code>, remove all occurrences of <code>val<\/code> in-place.\n    The order of the elements may be changed. Then return the number of elements in <code>nums<\/code> which are not equal to <code>val<\/code>.\n  <\/p>\n  <p>\n    Consider the number of elements in <code>nums<\/code> which are not equal to <code>val<\/code> be <code>k<\/code>. To get accepted, you need to:\n  <\/p>\n  <ul>\n    <li>Modify <code>nums<\/code> such that the first <code>k<\/code> elements contain elements not equal to <code>val<\/code>.<\/li>\n    <li>The remaining elements beyond <code>k<\/code> do not matter.<\/li>\n    <li>Return <code>k<\/code>.<\/li>\n  <\/ul>\n\n  <h2>Examples:<\/h2>\n\n  <p><strong>Example 1:<\/strong><\/p>\n  <pre><code>Input: nums = [3,2,2,3], val = 3\nOutput: 2, nums = [2,2,_,_]\nExplanation: The first 2 elements should be 2. Underscores represent irrelevant values.<\/code><\/pre>\n\n  <p><strong>Example 2:<\/strong><\/p>\n  <pre><code>Input: nums = [0,1,2,2,3,0,4,2], val = 2\nOutput: 5, nums = [0,1,4,0,3,_,_,_]\nExplanation: The first 5 elements should be any order of [0,1,4,0,3].<\/code><\/pre>\n\n  <h2>Approach: Two Pointer Technique<\/h2>\n  <ol>\n    <li>Use pointer <code>x<\/code> to track where the next non-<code>val<\/code> element should go.<\/li>\n    <li>Traverse the array with index <code>i<\/code>.<\/li>\n    <li>If <code>nums[i] != val<\/code>, assign <code>nums[x] = nums[i]<\/code> and increment <code>x<\/code>.<\/li>\n  <\/ol>\n\n  <h2>Dry Run<\/h2>\n  <pre><code>Input: nums = [0,1,2,2,3,0,4,2], val = 2\nx = 0\n\ni = 0 \u2192 nums[0] = 0 \u2260 2 \u2192 nums[0] = 0, x = 1\ni = 1 \u2192 nums[1] = 1 \u2260 2 \u2192 nums[1] = 1, x = 2\ni = 2 \u2192 nums[2] = 2 = 2 \u2192 skip\ni = 3 \u2192 nums[3] = 2 = 2 \u2192 skip\ni = 4 \u2192 nums[4] = 3 \u2260 2 \u2192 nums[2] = 3, x = 3\ni = 5 \u2192 nums[5] = 0 \u2260 2 \u2192 nums[3] = 0, x = 4\ni = 6 \u2192 nums[6] = 4 \u2260 2 \u2192 nums[4] = 4, x = 5\ni = 7 \u2192 nums[7] = 2 = 2 \u2192 skip\n\nResult: k = 5, nums = [0,1,3,0,4,...]<\/code><\/pre>\n\n  <h2>Complexity<\/h2>\n  <ul>\n    <li><strong>Time:<\/strong> O(N)<\/li>\n    <li><strong>Space:<\/strong> O(1)<\/li>\n  <\/ul>\n<\/div>\n\n<!-- \u2705 Code Tabs -->\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"csharp\">C#<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">var removeElement = function(nums, val) {\n  let x = 0;\n  for (let i = 0; i < nums.length; i++) {\n    if (nums[i] != val) {\n      nums[x] = nums[i];\n      x++;\n    }\n  }\n  return x;\n};<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">class Solution {\npublic:\n  int removeElement(vector&lt;int&gt;& nums, int val) {\n    int x = 0;\n    for (int i = 0; i < nums.size(); i++) {\n      if (nums[i] != val) {\n        nums[x] = nums[i];\n        x++;\n      }\n    }\n    return x;\n  }\n};<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">int removeElement(int* nums, int numsSize, int val) {\n  int x = 0;\n  for (int i = 0; i < numsSize; i++) {\n    if (nums[i] != val) {\n      nums[x] = nums[i];\n      x++;\n    }\n  }\n  return x;\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">public class Solution {\n  public int removeElement(int[] nums, int val) {\n    int x = 0;\n    for (int i = 0; i < nums.length; i++) {\n      if (nums[i] != val) {\n        nums[x] = nums[i];\n        x++;\n      }\n    }\n    return x;\n  }\n}<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">class Solution(object):\n  def removeElement(self, nums, val):\n    x = 0\n    for i in range(len(nums)):\n      if nums[i] != val:\n        nums[x] = nums[i]\n        x += 1\n    return x<\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"csharp\">\n    <pre><code class=\"language-csharp\">public class Solution {\n  public int RemoveElement(int[] nums, int val) {\n    int x = 0;\n    for (int i = 0; i < nums.Length; i++) {\n      if (nums[i] != val) {\n        nums[x] = nums[i];\n        x++;\n      }\n    }\n    return x;\n  }\n}<\/code><\/pre>\n  <\/div>\n<\/div>\n\n<!-- \u2705 JS Tab Switch Logic -->\n<script>\n  document.addEventListener('DOMContentLoaded', function () {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n        buttons.forEach(btn => btn.classList.remove('active'));\n        button.classList.add('active');\n        contents.forEach(content => {\n          content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n        });\n      });\n    });\n  });\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array nums and an integer val, remove all occurrences of val in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val. Consider the number of elements in nums which are not equal to val be k. To get accepted,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[811,810],"tags":[],"class_list":["post-6156","post","type-post","status-publish","format-standard","category-data-structures-and-algorithms","category-dsa"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6156"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6156\/revisions"}],"predecessor-version":[{"id":6438,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6156\/revisions\/6438"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}