当前位置:网站首页>347. top k high frequency elements
347. top k high frequency elements
2022-06-11 06:21:00 【Geek student】
347. front K High frequency elements
Give you an array of integers nums And an integer k , Please return to the frequency before k High element . You can press In any order Return to the answer .
It seems that the time complexity is not satisfied ,js Writing priority queues is too complicated , Wait and see
Ideas
First map once , Calculate the frequency of each number
Then arrange them in descending order of frequency Before the final election k individual
var topKFrequent = function (nums, k) {
let res = [];
const map = new Map();
for (const num of nums) {
map.set(num, (map.get(num) || 0) + 1);
}
// Returns the iteration object of an array , This object contains the key value pairs of the array (key,value)
// In descending order of frequency
let sortedArray = [...map.entries()].sort((a, b) => b[1] - a[1]);
for (let i = 0; i < k; i++) {
res.push(sortedArray[i][0]);
}
return res;
};
Chain writing :
var topKFrequent = function (nums, k) {
let res = [];
const map = new Map();
nums.forEach(num => map.set(num, (map.get(num) || 0) + 1));
return [...map.entries()]
.sort((a, b) => b[1] - a[1])
.map(x => x[0])
.slice(0, k);
}
边栏推荐
- Compliance management 101: processes, planning and challenges
- A collection of problems on improving working frequency and reducing power consumption in FPGA design
- What is sentinel produced by Ali?
- FPGA设计中提高工作频率及降低功耗题目合集
- Why is it that the live video of the devices connected to easygbs suddenly cannot be played? Insufficient database read / write
- MySQL implements over partition by (sorting the data in the group after grouping)
- Chapter 6 of machine learning [series] random forest model
- Sign for this "plug-in" before returning home for the new year
- Review Servlet
- [reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)
猜你喜欢
![Chapter 2 of machine learning [series] logistic regression model](/img/8f/b4c302c0309f5c91c7a40e682f9269.jpg)
Chapter 2 of machine learning [series] logistic regression model

Don't be afraid of xxE vulnerabilities: understand their ferocity and detection methods

jenkins-用户权限管理

Fix the problem that the right mouse button does not have a vscode shortcut

What should the cross-border e-commerce evaluation team do?

Eureka集群搭建

Deployment of Flink

JIRA software annual summary: release of 12 important functions

Why is it that the live video of the devices connected to easygbs suddenly cannot be played? Insufficient database read / write

FPGA设计——乒乓操作实现与modelsim仿真
随机推荐
Thymeleafengine template engine
FIFO最小深度计算的题目合集
A collection of problems on improving working frequency and reducing power consumption in FPGA design
FPGA设计中提高工作频率及降低功耗题目合集
PHP laravel8 send email
call和apply和bind的区别
Super (subclass)__ init__ And parent class__ init__ ()
Teach everyone how to implement an electronic signature
Instanceof and type conversion
Transfer Learning
Devsecops in Agile Environment
Principle of copyonwritearraylist copy on write
Review XML and JSON
Excellent practice | how to avoid a bloody case caused by a line of wrong code?
Global case | how an airline with a history of 100 years can expand and transform to promote innovation in the aviation industry
通过两种方式手写一个消息队列
Fix the problem that the right mouse button does not have a vscode shortcut
C language war "minesweeping"
What should the cross-border e-commerce evaluation team do?
Review Servlet