-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathUniqueOccurrenceChecker.java
More file actions
26 lines (19 loc) · 925 Bytes
/
UniqueOccurrenceChecker.java
File metadata and controls
26 lines (19 loc) · 925 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
package hash_map_set;
import java.util.*;
public class UniqueOccurrenceChecker {
public boolean hasUniqueOccurrences(int[] numbers) {
Map<Integer, Integer> numberCounts = new HashMap<>();
for (int num : numbers) {
numberCounts.put(num, numberCounts.getOrDefault(num, 0) + 1);
}
Set<Integer> uniqueCounts = new HashSet<>(numberCounts.values());
return numberCounts.size() == uniqueCounts.size();
}
public static void main(String[] args) {
UniqueOccurrenceChecker checker = new UniqueOccurrenceChecker();
assert checker.hasUniqueOccurrences(new int[]{1,2,2,1,1,3}) : "Test case 1 failed";
assert !checker.hasUniqueOccurrences(new int[]{1,2}) : "Test case 2 failed";
assert checker.hasUniqueOccurrences(new int[]{-3,0,1,-3,1,1,1,-3,10,0}) : "Test case 3 failed";
System.out.println("All test cases passed!");
}
}