Skip to content

Commit

Permalink
Add a C++ solution for ademclk#34
Browse files Browse the repository at this point in the history
  • Loading branch information
Legoeggolas committed Aug 21, 2022
1 parent 85dc649 commit 696f956
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions challenge/remove_duplicate_elements/solution.cpp
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 << " ";
}
}
*/
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 696f956

Please sign in to comment.