Reference: LeetCode
Difficulty: Easy

Problem

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

Note:

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

Example:

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

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

Follow-Up: If it is sorted, can we do it without extra space?

Analysis

Brute-Force

Use one set for the result list to avoid duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set = new HashSet<>();

for (int val1 : nums1) {
for (int val2 : nums2) {
if (val1 == val2) {
set.add(val1);
}
}
}
// to array
int[] result = new int[set.size()];
int count = 0;
for (int val : set) {
result[count] = val;
count += 1;
}
return result;
}

Time: $O(M \times N)$
Space: $O(M + N)$

Use Extra Sets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> interSet = new HashSet<>(); // use hash set to avoid duplicates
Set<Integer> set = new HashSet<>();
// add nums1 to set
for (int val : nums1) set.add(val);
// check if nums2 in set
for (int val : nums2) {
if (set.contains(val)) {
interSet.add(val);
}
}
// to array
int[] result = new int[interSet.size()];
int count = 0;
for (int val : interSet) {
result[count] = val;
++count;
}
return result;
}

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for (int val : nums1) set1.add(val);
for (int val : nums2) set2.add(val);
if (set1.size() < set2.size()) {
return intersectionHelper(set1, set2);
} else {
return intersectionHelper(set2, set1);
}
}

private int[] intersectionHelper(Set<Integer> set1, Set<Integer> set2) {
// assume set1.size() < set2.size()
int[] result = new int[set1.size()];
int count = 0;
for (int val : set1) {
if (set2.contains(val)) {
result[count] = val;
count += 1;
}
}
return Arrays.copyOf(result, count); // init with count-element array
}

Time: $O(M + N)$
Space: $O(M + N)$

Built-In Intersection

1
2
3
4
5
6
7
8
9
10
11
12
13
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for (int val : nums1) set1.add(val);
for (int val : nums2) set2.add(val);
set1.retainAll(set2);
int[] result = new int[set1.size()];
int count = 0;
for (int val : set1) {
result[count++] = val;
}
return result;
}

Time: $O(N + M)$
Space: $O(N + M)$

Two Pointers If Sorted

Note: Be careful of the conditions.

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
public int[] intersection(int[] nums1, int[] nums2) {
// Assume they are sorted
Arrays.sort(nums1);
Arrays.sort(nums2);

Set<Integer> result = new HashSet<>();

int n1 = nums1.length, n2 = nums2.length;
int i1 = 0, i2 = 0;
while (i1 < n1 && i2 < n2) {
while (i1 < n1 && i2 < n2 && nums1[i1] < nums2[i2]) ++i1;
while (i1 < n1 && i2 < n2 && nums1[i1] > nums2[i2]) ++i2;

// critical: consider nums1[i1] < nums2[i2] --> if it is, we should continue to the next loop
if (i1 < n1 && i2 < n2 && nums1[i1] == nums2[i2]) {
result.add(nums1[i1]); // we use hash set --> no need to handle duplicates
++i1;
++i2; // update indices should be inside the if logic
}
}

int[] output = new int[result.size()];
int i = 0;
for (int val : result) {
output[i] = val;
++i;
}

return output;
}