Skip to content

Commit 825f9dc

Browse files
adde solution for 0027-RemoveElement
1 parent e573f82 commit 825f9dc

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
---
2+
id: remove-element
3+
title: Remove Element (LeetCode)
4+
sidebar_label: 0027-RemoveElement
5+
difficulty: Easy
6+
---
7+
8+
## Problem Description
9+
10+
11+
| Problem Statement | Solution Link | LeetCode Profile |
12+
| :---------------- | :------------ | :--------------- |
13+
| [Merge Two Sorted Lists](https://leetcode.com/problems/remove-element/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/remove-element/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
14+
15+
## Problem Description
16+
17+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` in-place. The order of the elements may be changed. Then return the number of elements in `nums` which are not equal to `val`.
18+
19+
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:
20+
21+
- 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`.
22+
- Return `k`.
23+
24+
### Custom Judge
25+
26+
The judge will test your solution with the following code:
27+
28+
```
29+
int[] nums = [...]; // Input array
30+
int val = ...; // Value to remove
31+
int[] expectedNums = [...]; // The expected answer with correct length.
32+
// It is sorted with no values equaling val.
33+
34+
int k = removeElement(nums, val); // Calls your implementation
35+
36+
assert k == expectedNums.length;
37+
sort(nums, 0, k); // Sort the first k elements of nums
38+
for (int i = 0; i < actualLength; i++) {
39+
assert nums[i] == expectedNums[i];
40+
}
41+
```
42+
43+
If all assertions pass, then your solution will be accepted.
44+
45+
### Examples
46+
47+
#### Example 1
48+
49+
- **Input:** `nums = [3,2,2,3], val = 3`
50+
- **Output:** `2, nums = [2,2,_,_]`
51+
- **Explanation:** Your function should return `k = 2`, with the first two elements of `nums` being 2. It does not matter what you leave beyond the returned `k` (hence they are underscores).
52+
53+
#### Example 2
54+
55+
- **Input:** `nums = [0,1,2,2,3,0,4,2], val = 2`
56+
- **Output:** `5, nums = [0,1,4,0,3,_,_,_]`
57+
- **Explanation:** Your function should return `k = 5`, with the first five elements of `nums` containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned `k` (hence they are underscores).
58+
59+
### Constraints
60+
61+
- `0 <= nums.length <= 100`
62+
- `0 <= nums[i] <= 50`
63+
- `0 <= val <= 100`
64+
65+
### Approach
66+
67+
To solve the problem, we can use the following approach:
68+
69+
1. **Initialize Two Pointers:**
70+
- Use two pointers, `slow` and `fast`, where `slow` points to the beginning of the array and `fast` moves forward to explore the array.
71+
72+
2. **Iterate Through the Array:**
73+
- While `fast` is within the bounds of the array:
74+
- If `nums[fast]` is not equal to `val`, copy `nums[fast]` to `nums[slow]` and increment both pointers.
75+
- If `nums[fast]` is equal to `val`, just increment `fast`.
76+
77+
3. **Return Result:**
78+
- The new length of the array without the elements equal to `val` is given by `slow`.
79+
80+
### Solution Code
81+
82+
#### Python
83+
84+
```
85+
class Solution(object):
86+
def removeElement(self, nums, val):
87+
slow = 0
88+
89+
for fast in range(len(nums)):
90+
if nums[fast] != val:
91+
nums[slow] = nums[fast]
92+
slow += 1
93+
94+
return slow
95+
```
96+
97+
#### Java
98+
99+
```
100+
class Solution {
101+
public int removeElement(int[] nums, int val) {
102+
int slow = 0;
103+
104+
for (int fast = 0; fast < nums.length; fast++) {
105+
if (nums[fast] != val) {
106+
nums[slow] = nums[fast];
107+
slow++;
108+
}
109+
}
110+
111+
return slow;
112+
}
113+
}
114+
```
115+
116+
#### C++
117+
118+
```
119+
class Solution {
120+
public:
121+
int removeElement(vector<int>& nums, int val) {
122+
int slow = 0;
123+
124+
for (int fast = 0; fast < nums.size(); fast++) {
125+
if (nums[fast] != val) {
126+
nums[slow] = nums[fast];
127+
slow++;
128+
}
129+
}
130+
131+
return slow;
132+
}
133+
};
134+
```
135+
136+
### Conclusion
137+
138+
The above solution efficiently removes all occurrences of a specified value from an integer array in-place. It employs a two-pointer technique to achieve a time complexity of $O(N)$ and a space complexity of $O(1)$. This ensures that the algorithm can handle input sizes up to the upper limit specified in the constraints efficiently, providing a simple yet effective approach to solving the problem of removing an element from an array.

0 commit comments

Comments
 (0)