diff --git a/include/s0026_remove_duplicates_from_sorted_array.hpp b/include/s0026_remove_duplicates_from_sorted_array.hpp new file mode 100644 index 0000000..e046e01 --- /dev/null +++ b/include/s0026_remove_duplicates_from_sorted_array.hpp @@ -0,0 +1,13 @@ +#ifndef S0026_REMOVE_DUPLICATES_FROM_SORTED_ARRAY +#define S0026_REMOVE_DUPLICATES_FROM_SORTED_ARRAY + +#include + +using namespace std; + +class Solution { + public: + int removeDuplicates(vector& nums); +}; + +#endif diff --git a/src/s0026_remove_duplicates_from_sorted_array.cpp b/src/s0026_remove_duplicates_from_sorted_array.cpp new file mode 100644 index 0000000..6e1e5dd --- /dev/null +++ b/src/s0026_remove_duplicates_from_sorted_array.cpp @@ -0,0 +1,17 @@ +#include "s0026_remove_duplicates_from_sorted_array.hpp" + +int Solution::removeDuplicates(vector& nums) { + int size = nums.size(); + if (size == 0 || size == 1) { + return size; + } + int slow{0}; + for (int fast{1}; fast < size; ++fast) { + if (nums.at(fast) == nums.at(slow)) { + continue; + } else { + nums[++slow] = nums.at(fast); + } + } + return slow + 1; +} diff --git a/tests/s0026_remove_duplicates_from_sorted_array.cpp b/tests/s0026_remove_duplicates_from_sorted_array.cpp new file mode 100644 index 0000000..883b3a7 --- /dev/null +++ b/tests/s0026_remove_duplicates_from_sorted_array.cpp @@ -0,0 +1,17 @@ +#include "s0026_remove_duplicates_from_sorted_array.hpp" + +#include + +TEST(Problem26, Case1) { + vector i{1, 1, 2}; + int o{2}; + Solution solution; + EXPECT_EQ(solution.removeDuplicates(i), o); +} + +TEST(Problem26, Case2) { + vector i{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}; + int o{5}; + Solution solution; + EXPECT_EQ(solution.removeDuplicates(i), o); +}