229. Remove Element
Easy
In-place Compaction
Array
Problem
Given an integer array nums and an integer val, remove every occurrence of val conceptually in-place and return the remaining values while preserving their original relative order.
Examples
Example 1
Input: [3,2,2,3], 3
Output: [2,2]
Example 2
Input: [0,1,2,2,3,0,4,2], 2
Output: [0,1,3,0,4]
Example 3
Input: [1], 1
Output: []
Constraints
� 0 <= nums.length <= 100
� 0 <= nums[i] <= 50
� 0 <= val <= 100
Hints
?? Keep a write pointer for values that should remain.
?? Copy each value not equal to val into the next write position.
Expected Complexity
Time: O(n)
Space: O(1)
Follow-up
Can you perform the compaction with O(1) auxiliary space?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[3,2,2,3], 3Expected
[2,2]