Alexander Obregon's Substack

Java LeetCode Solutions

LeetCode #27: Remove Element — Solved in Java

Simple Write Pass and Two Pointers With Tail Swap + Bonus Solution

Alexander Obregon's avatar
Alexander Obregon
Oct 17, 2025
∙ Paid
LeetCode Logo
Image Source

LeetCode’s Remove Element problem asks for a method that removes all occurrences of a given value from an array, without creating a new one. The goal is to rearrange the numbers in place so that the first section of the array contains only the values that remain. The order of those values doesn’t matter, but you must return how many were kept at the front. Anything past that point is ignored when LeetCode checks your result. It’s a common warm-up question for learning how to move data efficiently without extra memory and helps build the kind of thinking used in more complex array problems later on.

Thinking about a solution in Java means focusing on how arrays behave in memory. Each element sits next to the others in a continuous block, so moving through them with indexes is fast and predictable. Every assignment to an index updates that specific slot instantly. That’s why problems like this reward careful control of indexes rather than extra containers or helper structures. The goal is to decide when to copy a value forward or swap it with another based on the current comparison.

LeetCode: Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums 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, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.

  • Return k.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int val = ...; // Value to remove
int[] expectedNums = [...]; // The expected answer with correct length.
                            // It is sorted with no values equaling val.

int k = removeElement(nums, val); // Calls your implementation

assert k == expectedNums.length;
sort(nums, 0, k); // Sort the first k elements of nums
for (int i = 0; i < actualLength; i++) {
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

Constraints:

  • 0 <= nums.length <= 100

  • 0 <= nums[i] <= 50

  • 0 <= val <= 100

Solution 1: Simple Write Pass

This version keeps a single write index and walks the array from left to right. Every value that does not equal the target gets copied forward. Elements equal to val get skipped. The front of the array ends up holding only the kept values, and the method returns how many were kept.

Keep reading with a 7-day free trial

Subscribe to Alexander Obregon's Substack to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Alexander Obregon
Privacy ∙ Terms ∙ Collection notice
Start your SubstackGet the app
Substack is the home for great culture