Arrays Intersection
Array, Hash Table
Given two integer arrays nums1
and nums2
, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
Example 1:
Example 2:
Constraints:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if num1's size is small compared to nums2's size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Solutions
Approach - Brute Force Technique
It works by checking each element of the first array against the second array and adding it to the intersection if it exists. The second array is represented as a list so that elements can be easily removed. This ensures that each element is counted the correct number of times.
Steps
Initialize intersection and list2: A new List
intersection
is created to store the intersection ofnums1
andnums2
. Another Listlist2
is created as a copy ofnums2
. This is done because elements will be removed fromlist2
during the process, and we don’t want to modify the original array.Iterate over nums1: The method iterates over each element
num
innums1
.Check if num exists in list2: For each
num
innums1
, the method checks ifnum
exists inlist2
using theContains
method.Add num to intersection and remove from list2: If
num
exists inlist2
, it is added tointersection
and removed fromlist2
. TheRemove
method removes the first occurrence ofnum
fromlist2
. This ensures that each element in the intersection appears as many times as it shows in both arrays.Return intersection as an array: After all elements in
nums1
have been processed, theToArray
method is called onintersection
to convert it back to an array before returning it as the result of the method.
Approach - Hash Table Technique
Steps
Initialization: The function
Intersect
is defined with two parameters,nums1
andnums2
, which are the input arrays. It initializes a dictionarydict
to keep track of the frequency of each number innums1
, and a listres
to store the result.Create Frequency Dictionary: The function iterates over each number in
nums1
. For each number, it checks if the number is already a key indict
. If it is, it increments the value associated with that key. If not, it adds the number as a key todict
with a value of 1. This results indict
containing each unique number innums1
as a key, and the frequency of that number as the value.Find Intersection: The function then iterates over each number in
nums2
. For each number, it checks if the number is a key indict
and if the associated value is greater than 0. If both conditions are met, it means the number is present in bothnums1
andnums2
, so it adds the number tores
and decrements the value associated with the number indict
.Return Result: Finally, the function converts
res
to an array and returns it. This array contains the intersection ofnums1
andnums2
.
Complexity
Time complexity:
O(N+M)
Space complexity:
O(N)
Last updated