-
Notifications
You must be signed in to change notification settings - Fork 56
/
Solution.java
33 lines (30 loc) · 932 Bytes
/
Solution.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 g0501_0600.s0532_k_diff_pairs_in_an_array;
// #Medium #Array #Hash_Table #Sorting #Binary_Search #Two_Pointers #Udemy_Arrays
// #2022_07_28_Time_13_ms_(58.23%)_Space_48.7_MB_(27.94%)
import java.util.HashSet;
public class Solution {
public int findPairs(int[] nums, int k) {
int res = 0;
HashSet<Integer> set = new HashSet<>();
HashSet<Integer> twice = new HashSet<>();
for (int n : nums) {
if (set.contains(n)) {
if (k == 0 && !twice.contains(n)) {
res++;
twice.add(n);
} else {
continue;
}
} else {
if (set.contains(n - k)) {
res++;
}
if (set.contains(n + k)) {
res++;
}
}
set.add(n);
}
return res;
}
}