File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments