-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodPairs.java
More file actions
29 lines (21 loc) · 765 Bytes
/
Copy pathGoodPairs.java
File metadata and controls
29 lines (21 loc) · 765 Bytes
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
package LeetcodePractice;
import java.util.HashMap;
public class GoodPairs {
public static int numIdenticalPairs(int[] nums) {
HashMap<Integer, Integer> countMap = new HashMap<>();
int goodPairs = 0;
for (int num : nums) {
goodPairs += countMap.getOrDefault(num, 0);
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
}
return goodPairs;
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 3, 1, 1, 3};
System.out.println(numIdenticalPairs(nums1));
int[] nums2 = {1, 1, 1, 1};
System.out.println(numIdenticalPairs(nums2));
int[] nums3 = {1, 2, 3};
System.out.println(numIdenticalPairs(nums3));
}
}