File tree Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Original file line number Diff line number Diff line change 44 * @return {number[] }
55 */
66var topKFrequent = function ( nums , k ) {
7- let map = new Map ( ) ;
7+ let map = { } ;
88 let res = [ ] ;
99 let bucket = Array . from ( { length : nums . length + 1 } , ( ) => [ ] ) ; // to create unique arrays
1010
1111 // storing frequency of numbers in a map
12- for ( let n of nums ) {
13- map . set ( n , map . has ( n ) ? 1 + map . get ( n ) : 1 ) ;
12+ for ( const n of nums ) {
13+ map [ n ] = ( n in map ) ? 1 + map [ n ] : 1 ;
1414 }
1515
16- // Poppulate the bucket with numbers in frequency
16+ // Populate the bucket with numbers in frequency
1717 // as the index of the bucket
18- for ( const [ key , value ] of map . entries ( ) ) {
19- bucket [ value ] . push ( key ) ;
18+ for ( const c in map ) {
19+ bucket [ map [ c ] ] . push ( c ) ;
2020 }
21-
21+
2222 for ( let i = bucket . length - 1 ; i >= 0 ; i -- ) {
2323 if ( bucket [ i ] . length > 0 ) {
24- for ( let n of bucket [ i ] ) {
25- res . push ( n ) ;
26- if ( res . length === k ) return res ;
24+ bucket [ i ] . forEach ( ( elem ) => res . push ( elem ) ) ;
25+ if ( k == res . length ) {
26+ return res ;
2727 }
2828 }
2929 }
You can’t perform that action at this time.
0 commit comments