Skip to content

Commit 33d2769

Browse files
committed
feat: Solving "Unique Number of Occurrences" (LeetCode | #1207 | Easy)
1 parent 128f0a7 commit 33d2769

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* LeetCode | #1207 | Easy
3+
*
4+
* Given an array of integers arr, return true if the number of occurrences of each
5+
* value in the array is unique or false otherwise.
6+
*
7+
* Constraints:
8+
* 1. 1 <= arr.length <= 1000
9+
* 2. -1000 <= arr[i] <= 1000
10+
*
11+
*/
12+
13+
/**
14+
* Time complexity: O(n) - Space complexity: O(n)?
15+
*
16+
* @param {number[]} arr
17+
* @return {boolean}
18+
*/
19+
function uniqueOccurrences(arr) {
20+
if (arr.length === 1) {
21+
return true;
22+
}
23+
24+
const occurrencesMap = new Map();
25+
for (const num of arr) {
26+
const occurrences = occurrencesMap.has(num)
27+
? occurrencesMap.get(num) + 1
28+
: 1;
29+
30+
occurrencesMap.set(num, occurrences);
31+
}
32+
const occurrencesSet = new Set([...occurrencesMap.values()]);
33+
34+
return occurrencesSet.size === occurrencesMap.size;
35+
};

0 commit comments

Comments
 (0)