Logo
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    Akhil Abraham
    Akhil Abraham
    /Posts
    Posts
    /LeetCode
    LeetCode
    /
    LeetCode
    /0349 - Intersection of Two Arrays
    0349 - Intersection of Two Arrays
    0349 - Intersection of Two Arrays
    0349 - Intersection of Two Arrays

    0349 - Intersection of Two Arrays

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/intersection-of-two-arrays/

    Solution 1 - Set Intersection

    Convert both arrays to sets and use Python's built-in set intersection operator. This leverages hash-based lookups to efficiently find common elements, returning only unique values.

    TimeComplexity:O(n+m)TimeComplexity: O(n + m)TimeComplexity:O(n+m)
    SpaceComplexity:O(n+m)SpaceComplexity: O(n + m)SpaceComplexity:O(n+m)

    Solution 2 - Hash Set with Manual Lookup

    Build a set from the first array, then iterate through the second array and collect elements found in the set. Use a separate result set to ensure uniqueness without relying on the built-in intersection operator.

    TimeComplexity:O(n+m)TimeComplexity: O(n + m)TimeComplexity:O(n+m)
    SpaceComplexity:O(n+m)SpaceComplexity: O(n + m)SpaceComplexity:O(n+m)
    """ 0349.1 - Intersection of Two Arrays - Solution 1 - Set Intersection """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
            """Intersection of Two Arrays Function"""
            return list(set(nums1) & set(nums2))
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().intersection([1, 2, 2, 1], [2, 2]))           # [2]
        print(Solution().intersection([4, 9, 5], [9, 4, 9, 8, 4]))     # [9, 4]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0349.2 - Intersection of Two Arrays - Solution 2 - Hash Set with Manual Lookup """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
            """Intersection of Two Arrays Function"""
            set1 = set(nums1)
            result = set()
    
            for num in nums2:
                if num in set1:
                    result.add(num)
    
            return list(result)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        print(Solution().intersection([1, 2, 2, 1], [2, 2]))           # [2]
        print(Solution().intersection([4, 9, 5], [9, 4, 9, 8, 4]))     # [9, 4]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()