当前位置:网站首页>实现一个方法,找出数组中的第k大和第m大的数字相加之和
实现一个方法,找出数组中的第k大和第m大的数字相加之和
2022-07-26 03:07:00 【二脸懵逼】
题目:实现一个方法,找出数组中的第k大和第m大的数字相加之和;
示例:
let arr = [1,2,4,4,3,5], k = 2, m = 4
findTopSum(arr, k, m); // 第2大的数是4,出现2次,第4大的是2,出现1次,所以结果为10
(自己写的,当然可能还有更简介的代码)
function bigSum(arr, k, m) {
//排序
arr = arr.sort(function (a, b) {
return b - a;
})
console.log('降序后的数组', arr)
//去重(是一个对象)
let newArr = new Set(arr);
//转为数组
let arr1 = [...newArr]
console.log('去重后的数组', arr1);
let temp1 = arr1[k - 1];
let temp2 = arr1[m - 1];
let total1 = 0;//计算相同元素的数量
let total2 = 0;
arr.forEach((item) => {
if (item == temp1) {
total1++;
}
if (item == temp2) {
total2++;
}
})
return (temp1 * total1 + temp2 * total2);
}
let result = bigSum([2, 4, 2, 8, 4, 2, 4, 5], 3, 4)
console.log('计算结果', result);

链接: js中数组去重的几种方法
边栏推荐
- 手把手教你依赖管理
- [sql] usage of self connection
- Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
- Win11麦克风权限的开启方法
- Golang 中‘...‘的用法
- Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
- Summary of Huawei virtualization fusioncompute knowledge points
- OxyCon 2022 网络抓取前沿大会即将开启!
- Swin Transformer【Backbone】
- ES6 set and map
猜你喜欢
![[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)](/img/b4/62a4c06743fdedacdffd9b156a760f.png)
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)

MySQL tutorial: MySQL database learning classic (from getting started to mastering)

LeetCode·每日一题·919.完全二叉树插入器·层次遍历·BFS

Opencv 以指定格式保存图片

VR panoramic shooting and production of business center helps businesses effectively attract people

File operation (I) -- File introduction and file opening and closing methods

记一次SQL优化

Multithreaded programming

Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation

图像识别(六)| 激活函数
随机推荐
Dataframe sorting: datetime format splitting; Delete a specific line; Group integration.
snownlp库各功能及用法
手把手教你依赖管理
JSD-2204-酷鲨商城(管理商品模块)-Day02
After clicking play, the variables in editorwindow will be destroyed inexplicably
Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic
STM32 - PWM learning notes
Win11隐藏输入法状态栏方法
[NOIP2001 普及组] 最大公约数和最小公倍数问题
微信公众号互助、开白群,小白报团取暖
GoLang 抽奖系统 设计
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (reconstruction)
ENVI_ Idl: create HDF5 file and write data (take writing GeoTIFF file to HDF file as an example) + detailed parsing
Skill list of image processing experts
Case: using kept+haproxy to build a Web Cluster
The source of everything, the choice of code branching strategy
GoLang日志编程系统