DSA · Array / String · #145
Remove Element
Module 73 · difficulty 2/5·⏱ 30:00starts on first keystroke
Given an integer array `nums` and an integer `val`, remove **all** occurrences of `val` in `nums` **in-place**. The relative order of the remaining elements may change. Implement `removeElement(nums, val)` which returns `k`, the number of elements that are **not** equal to `val`. The first `k` elements of `nums` must hold those remaining values (in any order); elements beyond index `k` do not matter. The function name to implement is `removeElement`.
Examples
nums = [3,2,2,3], val = 3→2, nums = [2,2,_,_]— Two elements differ from 3. The first two slots hold the kept values (order may vary).nums = [0,1,2,2,3,0,4,2], val = 2→5, nums = [0,1,3,0,4,_,_,_]— Five elements are not equal to 2.nums = [], val = 0→0— Empty array yields k = 0.
Constraints
- · 0 <= nums.length <= 100
- · 0 <= nums[i] <= 50
- · 0 <= val <= 100
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.