-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueOccurrences.java
More file actions
36 lines (26 loc) · 1 KB
/
Copy pathUniqueOccurrences.java
File metadata and controls
36 lines (26 loc) · 1 KB
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
34
35
36
package LeetcodePractice;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class UniqueOccurrences {
public boolean uniqueOccurrences(int[] arr) {
Map<Integer, Integer> countMap = new HashMap<>();
for (int num : arr) {
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
}
Set<Integer> occurrenceSet = new HashSet<>();
for (int count : countMap.values()) {
if (!occurrenceSet.add(count)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
UniqueOccurrences solution = new UniqueOccurrences();
System.out.println(solution.uniqueOccurrences(new int[]{1,2,2,1,1,3}));
System.out.println(solution.uniqueOccurrences(new int[]{1,2}));
System.out.println(solution.uniqueOccurrences(new int[]{-3,0,1,-3,1,1,1,-3,10,0}));
}
}