-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1207.java
More file actions
29 lines (25 loc) · 1016 Bytes
/
LeetCode1207.java
File metadata and controls
29 lines (25 loc) · 1016 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
import java.util.HashMap;
import java.util.HashSet;
public class LeetCode1207 {
public static void main(String[] args) {
// 输入:arr = [1,2,2,1,1,3]
// 输出:true
System.out.println(new Solution1207().uniqueOccurrences(new int[] { 1, 2, 2, 1, 1, 3 }));
// 输入:arr = [1,2]
// 输出:false
System.out.println(new Solution1207().uniqueOccurrences(new int[] { 1, 2 }));
// 输入:arr = [-3,0,1,-3,1,1,1,-3,10,0]
// 输出:true
System.out.println(new Solution1207().uniqueOccurrences(new int[] { -3, 0, 1, -3, 1, 1, 1, -3, 10, 0 }));
}
}
class Solution1207 {
public boolean uniqueOccurrences(int[] arr) {
HashMap<Integer, Integer> records = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
records.put(arr[i], records.getOrDefault(arr[i], 0) + 1);
}
HashSet<Integer> counts = new HashSet<>(records.values());
return counts.size() == records.size();
}
}