From 267e44a13741ff0756ff46eb5d8e4698e69a65e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:31:06 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=2027=20(Remove=20?= =?UTF-8?q?Element)=20from=20TypeScript=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces typescript/removeElement.ts with solutions/0001-0100/0027-remove-element/. --- .../0001-0100/0027-remove-element/README.md | 12 ++++++++++++ .../0001-0100/0027-remove-element/solution.cpp | 18 ++++++++++++++++++ .../0001-0100/0027-remove-element/solution.hpp | 10 ++++++++++ .../0001-0100/0027-remove-element/test.cpp | 11 +++++++++++ typescript/removeElement.ts | 11 ----------- 5 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 solutions/0001-0100/0027-remove-element/README.md create mode 100644 solutions/0001-0100/0027-remove-element/solution.cpp create mode 100644 solutions/0001-0100/0027-remove-element/solution.hpp create mode 100644 solutions/0001-0100/0027-remove-element/test.cpp delete mode 100644 typescript/removeElement.ts diff --git a/solutions/0001-0100/0027-remove-element/README.md b/solutions/0001-0100/0027-remove-element/README.md new file mode 100644 index 0000000..5da8b18 --- /dev/null +++ b/solutions/0001-0100/0027-remove-element/README.md @@ -0,0 +1,12 @@ +# 27. Remove Element + +[LeetCode problem 27](https://leetcode.com/problems/remove-element/) + +- **Difficulty**: Easy +- **Tags**: array, two-pointers + +## Description + +Given an array `nums` and a value `val`, remove all occurrences of `val` in +place and return the number of remaining elements `k`; the first `k` +elements of `nums` must hold the result, in any order. diff --git a/solutions/0001-0100/0027-remove-element/solution.cpp b/solutions/0001-0100/0027-remove-element/solution.cpp new file mode 100644 index 0000000..49b6d62 --- /dev/null +++ b/solutions/0001-0100/0027-remove-element/solution.cpp @@ -0,0 +1,18 @@ +#include "solution.hpp" + +namespace leetcode::p0027 { + +int solve(std::vector& nums, int val) { + int k = 0; + + for (int num : nums) { + if (num != val) { + nums.at(static_cast(k)) = num; + ++k; + } + } + + return k; +} + +} // namespace leetcode::p0027 diff --git a/solutions/0001-0100/0027-remove-element/solution.hpp b/solutions/0001-0100/0027-remove-element/solution.hpp new file mode 100644 index 0000000..2547a41 --- /dev/null +++ b/solutions/0001-0100/0027-remove-element/solution.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include + +namespace leetcode::p0027 { + +// optimized: O(n) time, O(1) extra space — overwrite in place, one pass. +int solve(std::vector& nums, int val); + +} // namespace leetcode::p0027 diff --git a/solutions/0001-0100/0027-remove-element/test.cpp b/solutions/0001-0100/0027-remove-element/test.cpp new file mode 100644 index 0000000..d95dd0d --- /dev/null +++ b/solutions/0001-0100/0027-remove-element/test.cpp @@ -0,0 +1,11 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 27", "[p0027]") { + std::vector a{3, 2, 2, 3}; + CHECK(leetcode::p0027::solve(a, 3) == 2); + + std::vector b{0, 1, 2, 2, 3, 0, 4, 2}; + CHECK(leetcode::p0027::solve(b, 2) == 5); +} diff --git a/typescript/removeElement.ts b/typescript/removeElement.ts deleted file mode 100644 index 986b550..0000000 --- a/typescript/removeElement.ts +++ /dev/null @@ -1,11 +0,0 @@ -const removeElement = function(nums: number[], val: number): number { - let k: number = 0; - for(const num of nums) { - if(num !== val) { - nums[k] = num; - k++; - } - } - - return k; -};