-
-
Notifications
You must be signed in to change notification settings - Fork 128
Added C++ vector manipulation snippets. #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,14 @@ tags: vector,remove,duplicate | |
#include <algorithm> | ||
#include <vector> | ||
|
||
void removeDublicates(std::vector<int> &input) | ||
void removeDuplicates(std::vector<int> &input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
std::sort(input.begin(), input.end()); // sort the vector | ||
auto last = std::unique(input.begin(), input.end()); // remove duplicates | ||
input.erase(last, input.end()); // resize vector and delete the undefined elements | ||
} | ||
|
||
// Usage: | ||
std::vector<int> vec = {4, 2, 2, 8, 5, 6, 9, 9, 9, 8, 8, 4}; | ||
removeDuplicates(vec); // returns {2, 4, 5, 6, 8, 9} | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to include an usage section as described in the our guidelines. See other snippets for reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry. I didn't see that I will do that |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,11 @@ void removeOccurrences(std::vector<int>& vec, int n) { | |
vec.end() | ||
); | ||
} | ||
|
||
// Usage: | ||
std::vector<int> vec = {4, 2, 4, 8, 5, 6, 8, 8, 4, 3 }; | ||
|
||
int n = 3; // Remove elements that occur exactly 3 times | ||
removeOccurrences(vec, n); // returns {2, 5, 6, 3} | ||
|
||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to include an usage section as described in the our guidelines. See other snippets for reference. |