Back to DSA sheet
Remove Element
EasyArraysGiven an array and a value val, remove every occurrence of val in place. The order of the remaining elements may change. Return the new length k; the first k positions must hold the kept elements.
Input format (stdin): the first line has n and val. The second line has n integers. Output k, then on the next line the first k kept values in order (print a blank line if k is 0).
Examples
Input: 4 3
3 2 2 3
Output: 2
2 2
Two non-3 values remain.
Input: 5 2
0 1 2 2 3
Output: 3
0 1 3
Keep 0, 1, 3.
Constraints
- 0 <= n <= 100
- 0 <= nums[i], val <= 50
Sheets
NeetCode 250
remove-element.cpp3 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.