Leetcode / 1207. Unique Number of Occurrences
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution {
public boolean uniqueOccurrences(int[] arr) {
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
HashSet<Integer> hashSet = new HashSet<Integer>();
for (int i = 0; i < arr.length; i++) {
if (hashMap.containsKey(arr[i])) {
hashMap.put(arr[i], hashMap.get(arr[i]) + 1);
}
else {
hashMap.put(arr[i], 1);
}
}
// Note: you can also use hashMap.values() instead of having to go through the key
for (int n : hashMap.keySet()) {
if (hashSet.contains(hashMap.get(n))) {
return false;
}
hashSet.add(hashMap.get(n));
}
return true;
}
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: