File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @description
3+ * time complexity: O(n log n)
4+ * space complexity: O(n)
5+ * runtime: 3ms
6+ * νμ΄ λ°©λ²:
7+ * ν΄μλ§΅μ ν΅ν΄ κ° μ«μμ λΉλμλ₯Ό κ³μ°
8+ * ν΄μλ§΅μ μ λ ¬νμ¬ λΉλμκ° λμ μμλλ‘ μ λ ¬
9+ * μ λ ¬λ λ°°μ΄μ kλ§νΌ μ§€λΌμ λ°ν
10+ * @param {number[] } nums
11+ * @param {number } k
12+ * @return {number[] }
13+ */
14+ const topKFrequent = function ( nums , k ) {
15+ const hashMap = new Map ( ) ;
16+
17+ for ( const num of nums ) {
18+ if ( hashMap . has ( num ) ) {
19+ hashMap . set ( num , hashMap . get ( num ) + 1 ) ;
20+ } else {
21+ hashMap . set ( num , 1 ) ;
22+ }
23+ }
24+
25+ return Array . from ( hashMap . entries ( ) )
26+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] )
27+ . slice ( 0 , k )
28+ . map ( ( [ num ] ) => num ) ;
29+ } ;
You canβt perform that action at this time.
0 commit comments