This code implements a solution to the "Intersection of Two Arrays II" problem, which is to find the intersection of two arrays and return it as an array. The intersection is defined as the elements that appear in both arrays, with their frequency taken into account.
Here's a step-by-step explanation of the code:
- We include the necessary headers:
vector
for dynamic arrays andunordered_map
for hash maps. - We use
using namespace std;
to avoid having to writestd::
before standard library entities. - The
intersect
function takes twovector<int>
parameters:nums1
andnums2
. - We create an
unordered_map<int, int>
calledcountMap
to store the frequency of each element innums1
. - We create a
vector<int>
calledresult
to store the intersection. - We iterate through
nums1
and increment the count of each element incountMap
. - We iterate through
nums2
:- For each element
num
innums2
, we check if it exists incountMap
and its count is greater than 0. - If the condition is true, we append
num
toresult
and decrement its count incountMap
.
- For each element
- Finally, we return the
result
vector containing the intersection.
- We import the necessary classes:
ArrayList
for dynamic arrays,HashMap
for hash maps, andList
for lists. - The
intersect
function takes twoint[]
parameters:nums1
andnums2
. - We create a
HashMap<Integer, Integer>
calledcountMap
to store the frequency of each element innums1
. - We create an
ArrayList
calledresult
to store the intersection. - We iterate through
nums1
and put each elementnum
incountMap
with its count. - We iterate through
nums2
:- For each element
num
innums2
, we check if it exists incountMap
and its count is greater than 0. - If the condition is true, we append
num
toresult
and decrement its count incountMap
.
- For each element
- Finally, we convert
result
to anint[]
array and return it.
- We define a function
intersect
that takes two parameters:nums1
andnums2
. - We create a
Map
calledcountMap
to store the frequency of each element innums1
. - We create an array called
result
to store the intersection. - We iterate through
nums1
and put each elementnum
incountMap
with its count. - We iterate through
nums2
:- For each element
num
innums2
, we check if it exists incountMap
and its count is greater than 0. - If the condition is true, we append
num
toresult
and decrement its count incountMap
.
- For each element
- Finally, we return the
result
array containing the intersection.
- We import
Counter
from thecollections
module to use a frequency map. - We define a class
Solution
with a methodintersect
that takes two parameters:nums1
andnums2
. - We create a
Counter
calledcountMap
to store the frequency of each element innums1
. - We create an array called
result
to store the intersection. - We iterate through
nums2
:- For each element
num
innums2
, we check if its count incountMap
is greater than 0. - If the condition is true, we append
num
toresult
and decrement its count incountMap
.
- For each element
- Finally, we return the
result
array containing the intersection.
- We define a function
intersect
that takes two parameters:nums1
andnums2
. - We create a
map[int]int
calledcountMap
to store the frequency of each element innums1
. - We create a slice called
result
to store the intersection. - We iterate through
nums1
and increment the count of each element incountMap
. - We iterate through
nums2
:- For each element
num
innums2
, we check if its count incountMap
is greater than 0. - If the condition is true, we append
num
toresult
and decrement its count incountMap
.
- For each element
- Finally, we return the
result
slice containing the intersection.
The time complexity of this solution is O(m+n), where m and n are the lengths of nums1
and nums2
, respectively. The space complexity is O(min(m,n)) since we use a hash map to store the frequency of elements from the smaller array.