Skip to content

Commit 47bfb1c

Browse files
committed
Add: 27. Remove Element
1 parent 7ebc2ef commit 47bfb1c

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

27-remove-element/README.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 27. Remove Element
2+
3+
## Description
4+
5+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` **in-place**. The order of the elements may be changed. Then return **the number of elements in `nums` which are not equal to `val`**.
6+
7+
Consider the number of elements in `nums` which are not equal to `val` to be `k`, to get accepted, you need to do the following things:
8+
9+
- Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
10+
- Return `k`.
11+
12+
---
13+
14+
## Custom Judge:
15+
16+
The judge will test your solution with the following code:
17+
18+
```
19+
int[] nums = [...]; // Input array
20+
int val = ...; // Value to remove
21+
int[] expectedNums = [...]; // The expected answer with correct length.
22+
// It is sorted with no values equaling val.
23+
24+
int k = removeElement(nums, val); // Calls your implementation
25+
26+
assert k == expectedNums.length;
27+
sort(nums, 0, k); // Sort the first k elements of nums
28+
for (int i = 0; i < actualLength; i++) {
29+
assert nums[i] == expectedNums[i];
30+
}
31+
```
32+
33+
If all assertions pass, then your solution will be accepted.
34+
35+
---
36+
37+
## Examples
38+
39+
### Example 1:
40+
41+
**Input:**
42+
```
43+
nums = [3,2,2,3], val = 3
44+
```
45+
**Output:**
46+
```
47+
2, nums = [2,2,_ ,_]
48+
```
49+
**Explanation:**
50+
Your function should return `k = 2`, with the first two elements of `nums` being `2`.
51+
It does not matter what you leave beyond the returned `k` (hence they are underscores).
52+
53+
---
54+
55+
### Example 2:
56+
57+
**Input:**
58+
```
59+
nums = [0,1,2,2,3,0,4,2], val = 2
60+
```
61+
**Output:**
62+
```
63+
5, nums = [0,1,4,0,3,_ ,_ ,_]
64+
```
65+
**Explanation:**
66+
Your function should return `k = 5`, with the first five elements of `nums` containing `0, 0, 1, 3, and 4`.
67+
Note that the five elements can be returned in **any order**.
68+
It does not matter what you leave beyond the returned `k` (hence they are underscores).
69+
70+
---
71+
72+
## Constraints
73+
74+
- `0 <= nums.length <= 100`
75+
- `0 <= nums[i] <= 50`
76+
- `0 <= val <= 100`

27-remove-element/index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const assert = require('assert');
2+
3+
/**
4+
* @param {number[]} nums
5+
* @param {number} val
6+
* @return {number}
7+
*/
8+
var removeElement = function (nums, val) {
9+
// Two-pointer approach: one pointer (w) writes, the other (r) reads.
10+
// The writer pointer (w) tracks the new valid array length.
11+
let w = 0;
12+
let r = 0;
13+
14+
// Iterate through the array using the reader pointer (r).
15+
while (r < nums.length) {
16+
if (nums[r] === val) {
17+
// If the current element is the target value, skip it.
18+
r++;
19+
} else {
20+
// Copy the element to the writer's position and move both pointers.
21+
// The writer pointer (w) only moves when we write a non-target value.
22+
nums[w++] = nums[r++];
23+
}
24+
}
25+
26+
// The final value of 'w' is the number of elements that are not 'val'.
27+
return w;
28+
};
29+
30+
// Example usage
31+
32+
/*
33+
Time Complexity: O(n)
34+
- We traverse the array once with the reader pointer (r), so the time complexity is O(n).
35+
36+
Space Complexity: O(1)
37+
- We modify the input array in-place without using extra space.
38+
- Only a few integer variables are used (constant space).
39+
*/

0 commit comments

Comments
 (0)