349 Intersection of Two Arrays - Easy

Problem:

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4] Explanation: [4,9] is also accepted.

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

Problem Analysis:

  1. High-Level Strategy:

    • The solution starts by converting nums1 and nums2 into sets using set(nums1) and set(nums2).
    • It then uses the intersection method of sets to find the common elements between the two sets.
    • Finally, it returns the resulting set, which contains the intersection of nums1 and nums2.
  2. Complexity Analysis:

    • Converting nums1 and nums2 into sets takes O(n) time complexity, where n is the size of the input lists.
    • The intersection operation on sets has an average time complexity of O(min(len(nums1), len(nums2))), as it iterates through the smaller set.
    • In the worst-case scenario where there are no common elements, the size of the resulting set is 0.
    • Overall, the time complexity of this solution is O(n) + O(min(len(nums1), len(nums2))), which simplifies to O(n), where n is the size of the larger input list.
    • The space complexity is O(len(nums1) + len(nums2)), as sets are created for both input lists. However, since the sets are temporary and not part of the final output, we can consider the space complexity as O(n), where n is the size of the larger input list.

Solutions:

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        #return set([value for value in nums1 if value in nums2])
        return set(nums1).intersection(set(nums2))

Similar Questions

  • 350-intersection-of-two-arrays-ii350 Intersection of Two Arrays II - EasyProblem: 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: Input: nums1 = \[1,2,2,1\], nums2 = \[2,2\] Output: \[2,2\] Example 2: Input: nums1 = \[4,9,5\], nums2 = \[9,4,9,8,4\] Output: \[4,9\] Explanation: \[9,4\] is also accepted. Constraints: * `1 List[int]: counter1 = Counter(nums1) counter2 = Counter(nu
  • 2248-intersection-of-multiple-arrays2248 Intersection of Multiple ArraysProblem: Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in *each array* of nums sorted in *ascending order*. Example 1: Input: nums = \[\[3,1,2,4,5\],\[1,2,3,4\],\[3,4,5,6\]\] Output: \[3,4\] Explanation: The only integers present in each of nums\[0\] = \[3,1,2,4,5\], nums\[1\] = \[1,2,3,4\], and nums\[2\] = \[3,4,5,6\] are 3 and 4, so we return \[3,4\]. Example 2: Input: nums = \[\[1,2,3\],\[4,5,
  • 2540-minimum-common-value2540 Minimum Common Value - EasyProblem: Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the *minimum integer common* to both arrays. If there is no common integer amongst nums1 and nums2, return -1. Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer. Example 1: Input: nums1 = \[1,2,3\], nums2 = \[2,4\] Output: 2 Explanation: The smallest element common to both arrays is 2, so we return 2. Example 2: Input: nums1 = \[1