forked from ademclk/callofcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85dc649
commit 696f956
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "solution.h" | ||
|
||
std::vector<int> remove_dupes(std::vector<int> const &vec) { | ||
std::unordered_set<int> seen; | ||
std::vector<int> uniques; | ||
|
||
for (auto const &num : vec) { | ||
if (seen.count(num) == 0) { | ||
uniques.push_back(num); | ||
seen.insert(num); | ||
} | ||
} | ||
|
||
return uniques; | ||
} | ||
|
||
/* | ||
int main() { | ||
std::vector<int> test = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 2, 2, 2, 2, 3, 4, 5}; | ||
auto temp = remove_dupes(test); | ||
// Output -> 1 2 3 4 5 | ||
for (auto const &num : temp) { | ||
std::cout << num << " "; | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef REM_DUP_H | ||
#define REM_DUP_H | ||
|
||
//#include <iostream> | ||
#include <unordered_set> | ||
#include <vector> | ||
|
||
/** | ||
* @brief Function to remove all duplicate elements from a vector. | ||
* Iterates over the elements and records the ones it sees. | ||
* Adds previously unseen elements to a new vector. | ||
* Returns this vector. | ||
* | ||
* @param vec The vector to remove the duplicates from | ||
* @return A vector with no duplicate elements. | ||
*/ | ||
std::vector<int> remove_dupes(std::vector<int> const &vec); | ||
|
||
#endif |