“The difference between the hunter and the prey is persistence.”

  • Sung Jin-Woo, moments before annihilating a dungeon boss

The Daily Quest: System Notification
Objective: Remove all occurrences of val from an array nums in-place.
Difficulty: 🟢 Level 2 (Beginner-Friendly But Tactical)
Reward: +15 DSA XP, +7 In-Place Operation Mastery

--
The Problem: A Dungeon Infested with "Val" Monsters
Imagine your array is a dungeon corridor filled with monsters (elements equal to val). Your mission: eliminate them all without using extra space (no portals or shadow summons allowed).

Example:
Input: nums = [3, 2, 2, 3], val = 3 → Output: 2 (nums becomes [2, 2, _, _])

Constraints:

Do it in O(n) time.

Use O(1) memory (no creating new arrays).

--
The Shadow Monarch’s Strategy: Two-Pointer Technique
Sung Jin-Woo doesn’t waste energy fighting every monster head-on. He strategically deploys his forces. Here’s how we mimicked his tactics:

  1. The Shadow Squad:

Scout (i): Traverses the dungeon (array) to identify threats.

Gatekeeper (k): Secures the safe zone by marking positions for non-monster elements.

  1. The Battle Plan:

Initialize k = 0 (Gatekeeper starts at the dungeon entrance).

Send the Scout (i) to inspect every element:

If nums[i] != val (not a monster), order the Gatekeeper to secure it at nums[k].

Advance the Gatekeeper (k++) to fortify the next position.

Return k (number of survivors).


Code Crafting: Commanding the Shadows

typescript
function removeElement(nums: number[], val: number): number {

let k = 0; // Gatekeeper starts at the gates

for (let i = 0; i < nums.length; i++) { // Scout explores the dungeon

if (nums[i] !== val) { // Found a human survivor!

nums[k] = nums[i]; // Gatekeeper secures them

k++; // Advance the safe zone

}

}

return k; // Total survivors

}

Live Battle Simulation:
Input: nums = [0, 1, 2, 2, 3, 0, 4, 2], val = 2

Scout (i=0): 0 is safe → Gatekeeper (k=0) secures it.

Scout (i=1): 1 is safe → Gatekeeper (k=1) secures it.

Scout (i=2): 2 is a monster → skipped.

Scout (i=6): 4 is safe → Gatekeeper (k=4) secures it.

Final Array: [0, 1, 3, 0, 4, _, _, _] → k = 5 survivors.

--
Why This Strategy Dominates

Time Complexity: O(n) — Scout clears the dungeon in one sweep.

Space Complexity: O(1) — No mana (memory) wasted.

This is the coding equivalent of Sung Jin-Woo’s Shadow Extraction skill — efficient, ruthless, and resourceful.


Level-Up Loot (Key Takeaways)

In-Place Operations Are King: Like Jin-Woo repurposing defeated shadows, we repurposed the array itself.

Order Doesn’t Matter: Focus on securing survivors, not their sequence (chaos is acceptable in battle).

Edge Cases:

Empty dungeon (array)? Return 0.

All monsters (elements = val)? Return 0.


Join the Shadow Army (Community Callout)

Upvote if you’d partner with Sung Jin-Woo in this coding dungeon!

Comment Below:

Your favorite in-place algorithm tactic.

Which Solo Leveling character best represents recursion? (Beru? Igris?)

Post your solution — let’s see who writes the cleanest code!

“Those who hesitate are disqualified.” Keep grinding, hunters! 🔥


Follow My Journey:

Reddit: Join r/CodeMonarch for daily updates.

Series Tag: #SoloCodingJourney

“The System is watching. Are you strong enough to answer?” 🕶️