LeetCode - Algorithms - 349. Intersection of Two Arrays

Problem

349. Intersection of Two Arrays

Java

brute force method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
final int len1 = nums1.length;
final int len2 = nums2.length;

Set<Integer> ret = new HashSet<Integer>();
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (nums1[i] == nums2[j])
ret.add(nums1[i]);
}
}
int[] r = new int[ret.size()];
int k = 0;
for (Integer e : ret)
r[k++] = e.intValue();
return r;
}
}

Submission Detail

  • 60 / 60 test cases passed.
  • Runtime: 8 ms, faster than 11.36% of Java online submissions for Intersection of Two Arrays.
  • Memory Usage: 39.9 MB, less than 54.69% of Java online submissions for Intersection of Two Arrays.

retainAll of HashSet in Java collections framework

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<Integer>();
Set<Integer> set2 = new HashSet<Integer>();
for (int i = 0; i < nums1.length; i++)
set1.add(nums1[i]);
for (int i = 0; i < nums2.length; i++)
set2.add(nums2[i]);
set1.retainAll(set2);
int[] r = new int[set1.size()];
int k = 0;
for (Integer e : set1)
r[k++] = e.intValue();
return r;
}
}

Submission Detail

  • 60 / 60 test cases passed.
  • Runtime: 2 ms, faster than 99.40% of Java online submissions for Intersection of Two Arrays.
  • Memory Usage: 39.4 MB, less than 90.00% of Java online submissions for Intersection of Two Arrays.