Skip to content

Commit e363606

Browse files
committed
Problem 177, remove an element from a list
1 parent 4c27c33 commit e363606

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 176 |
9+
| Total Problems | 177 |
1010

1111
</center>
1212

@@ -253,3 +253,4 @@ Include contains single header implementation of data structures and some algori
253253
| Count the number of islands in a grid. Given a grid representing 1 as land body, and 0 as water body, determine the number of islands (more details in problem comments)|[count_islands.cpp](leet_code_problems/count_islands.cpp)|
254254
| Find median from a data stream. Design a data structure that supports addNum to add a number to the stream, and findMedian to return the median of the current numbers seen so far. Also, if the count of numbers is even, return average of two middle elements, return median otherwise.|[median_stream.cpp](leet_code_problems/median_stream.cpp)
255255
| Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ) | [remove_invalid_parenthesis.cpp](leet_code_problems/remove_invalid_parenthesis.cpp)|
256+
| Given an array and a value, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length.| [remove_element.cpp](leet_code_problems/remove_element.cpp)|

leet_code_problems/median_stream.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include <queue>
5050
#include <vector>
5151
#include <random>
52+
#include <algorithm>
5253

5354
class MedianFinder {
5455
public:

leet_code_problems/remove_element.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Source LeetCode Problem 27 Remove element.
3+
* Given an array and a value, remove all instances of that value in-place and return the new length.
4+
* Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
5+
* The order of elements can be changed. It doesn't matter what you leave beyond the new length.
6+
*
7+
* Approach:
8+
*
9+
* The idea is to use two pointers, one iterates the entire nums(i), and the other one maintains the length of set of numbers
10+
* which we want in our output.(j). The idea is everytime we find a wanted element we move the j. This way j maintains all
11+
* the wanted element at top of the nums.
12+
*/
13+
14+
#include <iostream>
15+
#include <vector>
16+
17+
18+
int remove_element(std::vector<int>& nums, int value)
19+
{
20+
int j = 0;
21+
for (int i = 0; i < nums.size(); ++i) {
22+
if (nums[i] != value) {
23+
nums[j] = nums[i];
24+
j++;
25+
}
26+
}
27+
return j;
28+
}
29+
30+
void print_vector(std::vector<int>& nums, int count)
31+
{
32+
for (int i = 0; i < count; ++i) {
33+
std::cout << nums[i] << " ";
34+
}
35+
std::cout << std::endl;
36+
}
37+
38+
int main()
39+
{
40+
std::vector<int> nums = {2, 2, 3, 4, 4, 5, 6, 2, 4, 3, 2, 3};
41+
std::cout << "Vector: ";
42+
print_vector(nums, nums.size());
43+
int count = remove_element(nums, 2);
44+
std::cout << "Removing 2 from above vector: ";
45+
print_vector(nums, count);
46+
return 0;
47+
}

0 commit comments

Comments
 (0)