Intersection Of Two Arrays Problem


Description

LeetCode Problem 349.

Given two arrays, write a function to compute their intersection.

Example 1:

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

Example 2:

1
2
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Note:

  • Each element in the result must be unique.
  • The result can be in any order.


Solution

A straightforward approach is to use a hash table. We can use a hash table to store the elements appear in the two arrays. We iterate through the two arrays. If an element appears in the first array, we mark the value of that element in the hash table to be 0. If an element already appears in the first array, and also appears in the second array, we mark the value of that element in the hash table to be 1. For all elements that has a value to be 1 in the hash table will be outputed. By only using 0 and 1, we can avoid repitition of the elements in the final result.

The time complexity is O(n), and the space complexity is O(n).


Sample C++ Code

This is a C++ solution using the hash table approach. The time complexity is O(n), and the space complexity is O(n).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    map<int, int> ht;
    vector<int> ans;
    for (int i = 0; i < nums1.size(); i ++) {
        ht[nums1[i]] = 0;
    }
    for (int i = 0; i < nums2.size(); i ++) {
        if (ht.find(nums2[i]) != ht.end())
            ht[nums2[i]] = 1;
    }
    for (map<int, int>::iterator mit = ht.begin(); 
        mit != ht.end(); mit ++) {
        if (mit->second == 1)
            ans.push_back(mit->first);
    }
    return ans;
}

int main() {
    vector<int> nums1 = {1,2,2,1};
    vector<int> nums2 = {2,2};
    vector<int> res = intersection(nums1, nums2);
    for (int i = 0; i < res.size(); i ++) 
        cout << res[i] << " " << endl;
    return 0;
}




Related Posts

X Of A Kind In A Deck Of Cards Problem

LeetCode 914. In a deck of cards, each card has...

Word Subsets Problem

LeetCode 916. You are given two string arrays words1 and...

Unique Morse Code Words Problem

LeetCode 804. International Morse Code defines a standard encoding where...

Unique Email Addresses Problem

LeetCode 929. Every valid email consists of a local name...

Uncommon Words From Two Sentences Problem

LeetCode 884. A sentence is a string of single-space separated...

Triples With Bitwise And Equal To Zero Problem

LeetCode 982. Given an integer array nums, return the number...