File tree 1 file changed +16
-9
lines changed
scripts/algorithms/U/Unique Number of Occurrences
1 file changed +16
-9
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 54 ms (Top 59.88%) | Memory: 43.70 MB (Top 21.19%)
2
+
3
+ // time O(n) space O(n)
1
4
var uniqueOccurrences = function ( arr ) {
2
- const obj = { } ;
3
- // Creating hashmap to store count of each number
4
- arr . forEach ( val => obj [ val ] = ( obj [ val ] || 0 ) + 1 ) ;
5
- // Creating an array of the count times
6
- const val = Object . values ( obj ) . sort ( ( a , b ) => a - b ) ;
7
- // Now, just finding the duplicates
8
- for ( let i = 0 ; i < val . length - 1 ; i ++ ) {
9
- if ( val [ i ] === val [ i + 1 ] ) return false ;
5
+ const map = { }
6
+
7
+ for ( const number of arr ) {
8
+ if ( map [ number ] ) {
9
+ map [ number ] += 1
10
+ } else {
11
+ map [ number ] = 1
12
+ }
10
13
}
11
- return true ;
14
+
15
+ const frequencies = Object . values ( map )
16
+ const set = new Set ( frequencies )
17
+
18
+ return frequencies . length === set . size
12
19
} ;
You can’t perform that action at this time.
0 commit comments