Remove an element by value from a given vector in C++
So in this post, we will discuss about removing elements by value from a vector in C++. First, we will look at some inbuilt functions that perform this task. Then we will create our custom function that removes ‘k’ occurrences of a particular value from the vector.
Using .find() and .erase() method
The .find() method is used to find out the index of the first occurrence of a particular element in a vector. Once the position(index) of the element to be deleted is found, we can simply use the .erase() function to remove that element from the vector.
The code for performing the above task is as follows :
#include <bits/stdc++.h> using namespace std; int main() { vector<int>v = {1,2,2,3,3,3,4,5}; int ele = 2; auto it = find(v.begin(), v.end(),ele); v.erase(it); for(int i = 0; i<v.size(); i++){ cout<<v[i]<<" "; } return 0; }
1) Create a vector v.
2) Use the find function to find the first occurrence of the element ‘ele’ in the vector
3) Finally using the erase function, remove the element from the vector.
The output of the code looks like this :
1 2 3 3 3 4 5
Limitation: Only one occurrence of 2 gets deleted from the vector.
Using a custom function
We’ll be writing a function that removes ‘k’ occurrences of a particular element from the vector. The function looks something like this :
void remove_by_value(int value , vector<int>& v, int number_of_removals = 1e9){ vector<int>result; //the resulting vector int n = v.size(); //we remove all occurrenes of 'value' if 'number_of_removals' = 1e9 if(number_of_removals == 1e9){ for(int i = 0; i<n; i++){ if(v[i] != value){//leae out the values to be removed result.push_back(v[i]); } } } //we remove a particular amount of occurrences only else{ for(int i = 0; i<n; i++){ //if no more removals required, leave the remaining vector as it is. if(number_of_removals == 0){ for(int j = i; j<n; j++){ result.push_back(v[j]); } break; } else if(v[i] != value){//push the values to be removed to the new vector 'result' result.push_back(v[i]); } else{ number_of_removals--;//you have removed a required element } } } //clear v and insert new values from the 'result' vector v.clear(); for(int i = 0; i<result.size(); i++){ v.push_back(result[i]); } }
Explanation of the code:
1) Function takes 3 parameters, 2 of which are mandatory while the third one is optional(to remove all occurrences of the value).
2) We have a vector named ‘result’ that stores the modified vector after removing the desired elements.
3) The ‘if’ loop is executed if all occurrences of the value are to be removed.
4) Decrement ‘k‘ every time we encounter the value to be removed. This will remove only ‘k’ number of occurrences of the value.
5) Once ‘k’ becomes 0: remove no more values and print the remaining vector.
6) Lastly, clear the vector ‘v’ and refill it with the elements of ‘result’ vector.
Let’s test our code using some test-case :
int main() { vector<int>v = {1,2,2,3,3,3,4,5}; int ele = 3; int number_of_removals = 2; remove_by_value(ele, v, number_of_removals); for(int i = 0; i<v.size(); i++){ cout<<v[i]<<" "; } return 0; }
The output of the above code is :
1 2 2 3 4 5
You can use your test-cases to understand the function in-depth. That was all for this post, be sure to share this post with your friends. In case of any doubts, feel free to comment down below.
Leave a Reply