class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> set2 = new HashSet<>();
for (int n : nums1) {
set1.add(n);
}
for (int n : nums2) {
if (set1.contains(n)) {
set2.add(n);
}
}
int[] res = new int[set2.size()];
int idx = 0;
for (int n : set2) {
res[idx++] = n;
}
return res;
}
}
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while (n != 1 && !set.contains(n)) {
set.add(n);
n = getNext(n);
}
return n == 1;
}
private int getNext(int n) {
int res = 0;
while (n > 0) {
int tmp = n % 10;
res += tmp * tmp;
n /= 10;
}
return res;
}
}