💡
Exercise 13

Remove Element — The Bouncer Technique 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Time to learn one of the most elegant tricks in array manipulation: the read/write pointer technique. This is the baby version of a pattern you'll use over and over again. 🍼➡️💪

Picture a bouncer at a club. There's a line of people, but some don't have tickets. The bouncer's job: remove everyone without a ticket and shuffle the remaining people forward so there are no gaps. No extra line allowed — everything happens in-place. 🚪🕺

The Two-Pointer Setup:

  • Read pointer — walks through every person in line, one by one.
  • Write pointer — marks the next open spot where a "valid" person should stand.
  • If the read pointer sees someone WITH a ticket (not equal to the value we're removing), copy them to the write position and advance the write pointer.
  • If the read pointer sees someone WITHOUT a ticket (equal to val), just skip them — the write pointer stays put.
  • At the end, the write pointer tells you exactly how many valid people remain. 🎯

Let's trace through removing 3 from [3, 2, 2, 3]:

# Array: [3, 2, 2, 3], remove val = 3 # write = 0 # # read=0: nums[0]=3 → skip! (it's the value to remove) # read=1: nums[1]=2 → valid! nums[0]=2, write=1 → [2, 2, 2, 3] # read=2: nums[2]=2 → valid! nums[1]=2, write=2 → [2, 2, 2, 3] # read=3: nums[3]=3 → skip! # # write = 2 → new length is 2, valid portion: [2, 2] ✅

⏱ Time Complexity: O(n) — The read pointer visits each element exactly once.

💾 Space Complexity: O(1) — We're not creating a new array. Everything happens in-place with just two pointer variables. The bouncer doesn't need a second club! 🏗️

🔮 Why this matters: The next problem is Move Zeroes — which is basically this SAME technique, but instead of removing elements, you're partitioning them (non-zeroes to the front, zeroes to the back). If you get the read/write pointer down here, Move Zeroes will feel like a remix of a song you already know. 🎶

📋 Instructions
Write a function `remove_element(nums, val)` that removes all occurrences of `val` from `nums` **in-place**. - Use the **read/write pointer** technique. - Move all elements that are NOT equal to `val` to the front of the array. - Return the **new length** (how many valid elements remain). - Do NOT create a new array. Test it: ```python nums1 = [3, 2, 2, 3] k = remove_element(nums1, 3) print(f"New length: {k}, Array: {nums1[:k]}") nums2 = [0, 1, 2, 2, 3, 0, 4, 2] k = remove_element(nums2, 2) print(f"New length: {k}, Array: {nums2[:k]}") nums3 = [1, 1, 1] k = remove_element(nums3, 1) print(f"New length: {k}, Array: {nums3[:k]}") ```
Use a write pointer starting at 0. Loop through each index with a read pointer. If nums[read] != val, copy it to nums[write] and increment write. At the end, write IS the new length!
⚠️ Try solving it yourself first — you'll learn more!
def remove_element(nums, val):
    write = 0
    for read in range(len(nums)):
        if nums[read] != val:
            nums[write] = nums[read]
            write += 1
    return write

nums1 = [3, 2, 2, 3]
k = remove_element(nums1, 3)
print(f"New length: {k}, Array: {nums1[:k]}")

nums2 = [0, 1, 2, 2, 3, 0, 4, 2]
k = remove_element(nums2, 2)
print(f"New length: {k}, Array: {nums2[:k]}")

nums3 = [1, 1, 1]
k = remove_element(nums3, 1)
print(f"New length: {k}, Array: {nums3[:k]}")
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.