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

Longest Consecutive Sequence Problem

LeetCode 128. Given an unsorted array of integers nums, return...

Set Matrix Zeroes Problem

LeetCode 73. Given an m x n integer matrix matrix,...

Minimum Window Substring Problem

LeetCode 76. Given two strings s and t of lengths...

Search in Rotated Sorted Array Problem

LeetCode 36. Determine if a 9 x 9 Sudoku board...

Integer to Roman Problem

LeetCode 12. Given an integer, convert it to a roman...