Skip to content

Commit 8f375e4

Browse files
authored
Remove Element Pull Request (codedecks-in#127)
* added solution to remove-element problem * Updated Readme file with Remove-Element problem Updated Readme with Remove-Element problem & user credentials
1 parent f30d14a commit 8f375e4

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

C++/remove-element.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//O(n) time complexity and O(1) space complexity
2+
//traverse array with fast pointer until the end
3+
//if the value at fast pointer isn't equal to value, then swap it with whatever is at slow pointer. Increment slow after the swap.
4+
//the value of slow will give the final length of the array as requested
5+
6+
class Solution {
7+
public:
8+
int removeElement(vector<int>& nums, int val) {
9+
int slow=0;
10+
int fast = 0;
11+
12+
13+
while (fast < nums.size()){
14+
15+
if (nums[fast]!=val){nums[slow++]=nums[fast];}
16+
fast++;
17+
18+
}
19+
20+
return slow;
21+
22+
}
23+
};

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
125125
| 496 | [next-greater-element-i](https://leetcode.com/problems/next-greater-element-i) | [Python](./Python/496_nextgreaterelement.py) | O(N) | O(1) | Medium | Array |
126126
| 1470 | [Shuffle the Array](https://leetcode.com/problems/shuffle-the-array) | [Java](./Java/shuffle-the-array.java) | O(N) | O(1) | Easy | Array |
127127
| 283 | [Move-Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./C++/Move-Zeroes.cpp) | O(N) | O(1) | Easy | Array |
128+
| 27 | [Remove-Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) | O(N) | O(1) | Easy | Array |
128129

129130
<br/>
130131
<div align="right">
@@ -442,6 +443,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
442443
| [Meli Haktas](https://github.com/MercerFrey) <br> <img src="https://avatars1.githubusercontent.com/u/29127873?s=460&u=149319db4468ec2316e49a75eb5e05b35eb05eef&v=4" width="100" height="100"> | Turkey | python | [Github](https://github.com/MercerFrey) |
443444
| [Saurav Prateek](https://github.com/SauravP97) <br> <img src="https://avatars3.githubusercontent.com/u/26816418?s=460&u=4b5222d5b6db79389efba062714c9dfba263732f&v=4" width="100" height="100"> | India | Java | [Github](https://github.com/SauravP97) <br> [Codechef](https://www.codechef.com/users/srvptk) <br> [Codeforces](https://codeforces.com/profile/srvptk97) <br> [Leetcode](https://leetcode.com/srvptk97) |
444445
| [Anushka Verma](https://github.com/verma-anushka) <br> <img src="https://avatars0.githubusercontent.com/u/46157435?s=400&u=aa3db9cf25b4a9644a16607d0c8bd6c98763a62d&v=4" width="100" height="100"> | India | C++ | [Portfolio](https://verma-anushka.github.io/anushkaverma/) <br> [LeetCode](https://leetcode.com/anushka_verma/) |
446+
| [James H](https://github.com/HoangJames) <br> <img src="https://avatars2.githubusercontent.com/u/15057250?s=460&v=4" width="100" height="100"> | United Kingdom | C++ | [Github](https://github.com/HoangJames) |
445447

446448
<br/>
447449
<div align="right">

0 commit comments

Comments
 (0)