DEV Community

Cover image for LeetCode Challenge: 27. Remove Element - JavaScript Solution πŸš€
Rahul Kumar Barnwal
Rahul Kumar Barnwal

Posted on • Edited on

LeetCode Challenge: 27. Remove Element - JavaScript Solution πŸš€

Top Interview 150

Arrays are a fundamental concept in programming, and learning how to modify them in-place is an essential skill. Let's dive into LeetCode 27: Remove Element, an interesting problem that sharpens your understanding of array manipulation.


πŸš€ Problem Description

Given an integer array nums and an integer val, your task is to:

  1. Remove all occurrences of val from nums in-place.
  2. Return the number of elements in nums that are not equal to val. The resulting array should contain the elements that are not equal to val in the first k positions, where k is the returned count. The remaining elements are irrelevant and can be ignored.

πŸ’‘ Examples

Example 1

Input: nums = [3,2,2,3], val = 3  
Output: 2, nums = [2,2,_,_]
Enter fullscreen mode Exit fullscreen mode

Example 2

Input: nums = [0,1,2,2,3,0,4,2], val = 2  
Output: 5, nums = [0,1,4,0,3,_,_,_]
Enter fullscreen mode Exit fullscreen mode

🧠 Key Insights

  • In-place modification: The problem requires you to modify the array without using extra space.
  • Two-pointer technique: This is a common approach to efficiently process arrays while minimizing extra memory usage.

πŸ† JavaScript Solution: Two-Pointer Approach

Here’s a clean and efficient implementation using the two-pointer technique:

var removeElement = function(nums, val) {
    let k = 0; // Points to the next position to place a valid element

    for (let i = 0; i < nums.length; i++) {
        if (nums[i] !== val) {
            nums[k] = nums[i];
            k++;
        }
    }

    return k;
};
Enter fullscreen mode Exit fullscreen mode

πŸ” How It Works

  1. Use a pointer k to track the next position for valid elements.
  2. Loop through the array:
    • If the current element (nums[i]) is not equal to val, copy it to nums[k] and increment k.
  3. Return k, the count of elements not equal to val.

πŸ”‘ Complexity Analysis

Time Complexity: 𝑂(𝑛), where 𝑛 is the length of nums. The array is traversed once.
Space Complexity: O(1), as we modify the array in-place without extra memory.


πŸ“‹ Dry Run

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

Dry Run

Output: k = 5, nums = [0,1,3,0,4,_,_,_]


✨ Pro Tips for Interviews

  1. Clarify requirements: Ask if modifying the array order is allowed (it is for this problem).
  2. Edge cases:
    • Empty array (nums = []).
    • val not in nums.
    • All elements equal to val.

πŸ“ Summary

The two-pointer technique is a powerful strategy for array problems requiring in-place modifications. By understanding how to use this pattern, you can tackle many similar challenges efficiently.


πŸ“š Learn More

Check out the full explanation and code walkthrough on Dev.to:
πŸ‘‰ Merge Sorted Array - JavaScript Solution

How would you solve this problem? Drop your ideas or alternate approaches in the comments! πŸš€

JavaScript #LeetCode #CodingInterview #ProblemSolving

Top comments (1)

Collapse
 
rahulgithubweb profile image
Rahul Kumar Barnwal

Follow Me on GitHub πŸš€

If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.

Don't forget to follow for more updates!