DEV Community

Cover image for 🧩 LeetCode Challenge: Remove Element | Top Interview Questions [Java Solution]
Rahul Kumar Barnwal
Rahul Kumar Barnwal

Posted on

🧩 LeetCode Challenge: Remove Element | Top Interview Questions [Java Solution]

Introduction 🚀

In this classic LeetCode problem, you’ll be working with arrays in a practical way to remove specific values in-place while maintaining the order of remaining elements.


Problem Statement đź“ť

You are given an integer array nums and an integer val. Remove all occurrences of val from nums in-place and return the number of elements remaining that are not equal to val.

Custom Requirements:

  • Modify the input array such that the first k elements contain values not equal to val.
  • The remaining elements in the array beyond k are irrelevant.
  • Return k, the count of elements not equal to val.

đź’ˇExamples

Example 1
Input:

nums = [3,2,2,3], val = 3
Enter fullscreen mode Exit fullscreen mode

Output:

k = 2, nums = [2,2,_,_]
Enter fullscreen mode Exit fullscreen mode

Explanation: The first two elements of nums are updated to [2,2], ignoring the rest.

Example 2
Input:

nums = [0,1,2,2,3,0,4,2], val = 2
Enter fullscreen mode Exit fullscreen mode

Output:

k = 5, nums = [0,1,4,0,3,_,_,_]
Enter fullscreen mode Exit fullscreen mode

Explanation: After removing all instances of 2, we are left with five valid elements in the array.


Approch 🧠

To solve this problem efficiently:

  1. Use two pointers:
    • A slow pointer to track the position where the next valid element will go.
    • A fast pointer to iterate through the array.
  2. If the current element is not equal to val, move it to the position tracked by the slow pointer and increment the slow pointer.
  3. Continue until the fast pointer reaches the end of the array.

Java Solutionđź’ˇ

Here’s the implementation:

class Solution {
    public int removeElement(int[] nums, int val) {
        int k = 0;

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

        return k;
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation of Code 🔍

  1. Initialization:

    • k is initialized to 0, representing the position of the next valid element.
  2. Iteration:
    Traverse through the array with a loop:

    • If nums[i] is not equal to val, update the nums[k] position and increment k.
  3. Output:
    Return the value of k as the count of valid elements in the array.


Complexity Analysis 🔧

  1. Time Complexity: O(n), where n is the length of the array. The algorithm iterates through the array once.

  2. Space Complexity: O(1), as the solution modifies the array in place without using additional space.


Step-by-Step Execution

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

  • Start: k = 0
  • Iteration: Remove Element Output: k = 5, nums = [0,1,3,0,4,_,_,_]

Conclusion ✨

The Remove Element problem is a classic array manipulation challenge that demonstrates the power of two-pointer techniques for in-place modifications. By iterating through the array just once, we can efficiently remove all occurrences of a target value while keeping the remaining elements intact.


Key takeaways 🎯

  1. Time efficiency: The solution has a linear time complexity of O(n), making it optimal for the given constraints.
  2. Space efficiency: No additional data structures are used, ensuring constant space usage (O(1)).
  3. Real-world relevance: Similar techniques are used in scenarios like filtering data streams, cleaning up lists, or processing arrays with constraints.

With its simplicity and practical utility, this problem helps solidify your understanding of basic array manipulation and pointer-based algorithms—critical skills for coding interviews and problem-solving in real-world scenarios.


Like and Share

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!