From 22e17378738d44abc08b1d955081acbd2b36b2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:26:00 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=20350=20(Intersec?= =?UTF-8?q?tion=20of=20Two=20Arrays=20II)=20from=20Swift=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/350.swift with solutions/0301-0400/0350-intersection-of-two-arrays-ii/. --- .../README.md | 12 +++++++++ .../solution.cpp | 25 +++++++++++++++++++ .../solution.hpp | 11 ++++++++ .../test.cpp | 8 ++++++ swift/350.swift | 19 -------------- 5 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 solutions/0301-0400/0350-intersection-of-two-arrays-ii/README.md create mode 100644 solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.cpp create mode 100644 solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.hpp create mode 100644 solutions/0301-0400/0350-intersection-of-two-arrays-ii/test.cpp delete mode 100644 swift/350.swift diff --git a/solutions/0301-0400/0350-intersection-of-two-arrays-ii/README.md b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/README.md new file mode 100644 index 0000000..bdb768a --- /dev/null +++ b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/README.md @@ -0,0 +1,12 @@ +# 350. Intersection of Two Arrays II + +[LeetCode problem 350](https://leetcode.com/problems/intersection-of-two-arrays-ii/) + +- **Difficulty**: Easy +- **Tags**: array, hash-table, two-pointers, sorting + +## Description + +Given two integer arrays `nums1` and `nums2`, return an array of their +intersection, where each element in the result appears as many times as it +shows in both arrays (order doesn't matter). diff --git a/solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.cpp b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.cpp new file mode 100644 index 0000000..6ec76c6 --- /dev/null +++ b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.cpp @@ -0,0 +1,25 @@ +#include "solution.hpp" + +#include + +namespace leetcode::p0350 { + +std::vector solve(const std::vector& nums1, const std::vector& nums2) { + std::unordered_map counts; + for (int value : nums1) { + ++counts[value]; + } + + std::vector result; + for (int value : nums2) { + auto it = counts.find(value); + if (it != counts.end() && it->second > 0) { + result.push_back(value); + --it->second; + } + } + + return result; +} + +} // namespace leetcode::p0350 diff --git a/solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.hpp b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.hpp new file mode 100644 index 0000000..a539698 --- /dev/null +++ b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace leetcode::p0350 { + +// optimized: O(n + m) time, O(min(n, m)) space — count occurrences of the +// smaller array, then consume counts while scanning the other. +std::vector solve(const std::vector& nums1, const std::vector& nums2); + +} // namespace leetcode::p0350 diff --git a/solutions/0301-0400/0350-intersection-of-two-arrays-ii/test.cpp b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/test.cpp new file mode 100644 index 0000000..2273058 --- /dev/null +++ b/solutions/0301-0400/0350-intersection-of-two-arrays-ii/test.cpp @@ -0,0 +1,8 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 350", "[p0350]") { + CHECK(leetcode::p0350::solve({1, 2, 2, 1}, {2, 2}) == std::vector{2, 2}); + CHECK(leetcode::p0350::solve({4, 9, 5}, {9, 4, 9, 8, 4}) == std::vector{9, 4}); +} diff --git a/swift/350.swift b/swift/350.swift deleted file mode 100644 index 31d07a1..0000000 --- a/swift/350.swift +++ /dev/null @@ -1,19 +0,0 @@ -class Solution { - func intersect(_ nums1: [Int], _ nums2: [Int]) -> [Int] { - var countMap = [Int: Int]() - var intersection = [Int]() - - for value1 in nums1 { - countMap[value1, default: 0] += 1 - } - - for value2 in nums2 { - if let count = countMap[value2], count > 0 { - intersection.append(value2) - countMap[value2] = count - 1 - } - } - - return intersection - } -} \ No newline at end of file