32. Strike a Value
A recalled product has the batch code val, and a warehouse keeps an inventory list of batch codes in the order the boxes are stored. All boxes with the recalled code must be taken off the list, while the remaining boxes stay in the same relative order.
Write a method removeValue that takes the array nums of batch codes and the integer val, and returns a new array with every occurrence of val removed. The result may be empty.
Example 1
- Input:
- nums = [3,2,2,3], val = 3
- Output:
- [2,2]
- Explanation:
Both boxes coded 3 are removed, leaving [2, 2].
Example 2
- Input:
- nums = [0,1,2,2,3,0,4,2], val = 2
- Output:
- [0,1,3,0,4]
- Explanation:
The three boxes coded 2 are removed and the others keep their order: [0, 1, 3, 0, 4].
Constraints
1 ≤ nums.length ≤ 1000
-109 ≤ nums[i], val ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)