You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
vartopKFrequent=function(nums,k){constmap=newMap();constres=[];constfreqArr=[];for(letnumofnums){map.set(num,(map.get(num)||0)+1);}for(let[num,freq]ofmap){freqArr[freq]=(freqArr[freq]||newSet()).add(num);}for(leti=freqArr.length-1;i>=0;i--){// if(freqArr[I]) is necessary because not all index has element attached to itif(freqArr[i])res.push(...freqArr[i]);if(res.length===k)break;}returnres;};
Time complexity : O(n)
The text was updated successfully, but these errors were encountered:
kyxg
added a commit
to kyxg/FrontEndCollection
that referenced
this issue
Jan 4, 2022
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Time complexity : O(n)
The text was updated successfully, but these errors were encountered: