1.Intersection of Two Arrays

Type I

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]

Solution: (Used hashset)

class Solution
{
public:
    vector<int> intersection(vector<int> &nums1, vector<int> &nums2)
    {

        vector<int> v;
        unordered_set<int> s;
        unordered_set<int> vec;
        int k;

        if (nums1.size() >= nums2.size())
        {
            for (int i = 0; i < nums1.size(); i++)
            {
                s.insert(nums1[i]);
            }
            k = 1;
        }
        else
        {
            for (int i = 0; i < nums2.size(); i++)
            {
                cout<<nums2[i];
                s.insert(nums2[i]);
            }
            k = 2;
        }
        
        if (k == 1)
        {
            for (int i = 0; i < nums2.size(); i++)
            {
                if (s.find(nums2[i]) != s.end())
                {
                    if (vec.find(nums2[i]) == vec.end())
                    {
                        vec.insert(nums2[i]);
                        v.push_back(nums2[i]);
                    }
                }
            }
        }
        else if(k==2)
        {
            for (int i = 0; i < nums1.size(); i++)
            {
                if (s.find(nums1[i]) != s.end())
                {
                    if (vec.find(nums1[i]) == vec.end())
                    {
                        vec.insert(nums1[i]);
                        v.push_back(nums1[i]);
                    }
                }
            }
        }

        return v;
    }
};

Time complexity O(n+m)

Type II

Each element in the result should appear as many times as it shows in both arrays.

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]

Solution: (Using hashmaps)

class Solution
{
public:
    vector<int> intersect(vector<int> &nums1, vector<int> &nums2)
    {

        unordered_map<int, int> m;
        vector<int> v;
        int k = 0;

        if (nums1.size() > nums2.size())
        {
            for (int i = 0; i < nums1.size(); i++)
            {
                if (m.find(nums1[i]) == m.end())
                {
                    m.insert({nums1[i], 1});
                }
                else
                {
                    m[nums1[i]]++;
                }
            }
            k = 1;
        }
        else
        {
            for (int i = 0; i < nums2.size(); i++)
            {
                if (m.find(nums2[i]) == m.end())
                {
                    m.insert({nums2[i], 1});
                }
                else
                {
                    m[nums2[i]]++;
                }
            }
        }

        if (k == 1)
        {
            for (int i = 0; i < nums2.size(); i++)
            {
                if (m.find(nums2[i]) != m.end())
                {
                    if (m[nums2[i]] > 0)
                    {
                        v.push_back(nums2[i]);
                        m[nums2[i]]--;
                    }
                }
            }
        }
        else
        {
            for (int i = 0; i < nums1.size(); i++)
            {
                if (m.find(nums1[i]) != m.end())
                {
                    if (m[nums1[i]] > 0)
                    {
                        v.push_back(nums1[i]);
                        m[nums1[i]]--;
                    }
                }
            }
        }

        return v;
    }
};

Last updated