Skip to content

Commit

Permalink
Merge pull request #35 from Legoeggolas/main
Browse files Browse the repository at this point in the history
Add a C++ solution for #34
  • Loading branch information
ademclk authored Aug 21, 2022
2 parents 85dc649 + 06ace5c commit 62b142b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
29 changes: 29 additions & 0 deletions challenge/remove_duplicate_elements/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#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 << " ";
}
return 0;
}
*/
19 changes: 19 additions & 0 deletions challenge/remove_duplicate_elements/solution.h
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

0 comments on commit 62b142b

Please sign in to comment.