forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2540.java
33 lines (31 loc) · 891 Bytes
/
_2540.java
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
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.Set;
public class _2540 {
public static class Solution1 {
public int getCommon(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
for (int num : nums1) {
set1.add(num);
}
Set<Integer> set2 = new HashSet<>();
for (int num : nums2) {
set2.add(num);
}
int result = -1;
for (int num : nums1) {
if (set2.contains(num)) {
result = num;
break;
}
}
for (int num : nums2) {
if (set1.contains(num) && result > num) {
result = num;
break;
}
}
return result;
}
}
}