Skip to content

Practice Remove Element

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#27
LeetCode Problem

Remove Element

Problem Statement

Remove occurrences of val from nums in place and return the number k of retained elements. Rearrange nums so that its first k positions contain exactly the original elements unequal to val, with their original multiplicities. Their order may change. Values after position k - 1 and the physical length of the array do not matter to the result.

Source: Remove Element on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [3,2,2,3], val = 3
Output
2, nums = [2,2,_,_]

# Example 2

Input
nums = [0,1,2,2,3,0,4,2], val = 2
Output
5, nums = [0,1,4,0,3,_,_,_]

Constraints

0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100

Related LeetCode Problems

Practice more problems similar to Remove Element.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.