当前位置:网站首页>实现一个方法,找出数组中的第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中数组去重的几种方法
边栏推荐
- Cloud native guide what is cloud native infrastructure
- Safety margin of mass consumption
- [sql] case expression
- Win11隐藏输入法状态栏方法
- 移位距离和假设的应用
- 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
- [C language] deeply understand integer lifting and arithmetic conversion
- 在混合云中管理数据库:八个关键注意事项
- VOFA+ 串口调试助手
猜你喜欢

Summary of Huawei virtualization fusioncompute knowledge points
![[translation] cloud like internal load balancer for kubernetes?](/img/e5/f003ebed05a94d2936cfef5617f745.jpg)
[translation] cloud like internal load balancer for kubernetes?

YOLOv3: An Incremental Improvement
![[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)](/img/b8/56c4541602c5a6e787e2455f80febd.png)
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)

STM32 - DMA notes

Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
![[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)](/img/1e/33f9cc9446dcad8cdb78babbb5a22c.jpg)
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)

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

【尤里复裂人】带你轻松理解——深拷贝和浅拷贝

从各大APP年度报告看用户画像——标签,比你更懂你自己
随机推荐
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
图像识别(六)| 激活函数
Usage of arguments.callee
Personally test five efficient and practical ways to get rid of orders, and quickly collect them to help you quickly find high-quality objects!
STM32 - PWM learning notes
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
Parallelloopbody in opencv
(pc+wap) dream weaving template vegetable and fruit websites
重装Win7系统如何进行?
From the annual reports of major apps, we can see that user portraits - labels know you better than you do
Jenkins' study notes are detailed
Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
Arthas download and startup
YOLOv3: An Incremental Improvement
LeetCode·83双周赛·6128.最好的扑克手牌·模拟
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
FPGA_ Initial use process of vivado software_ Ultra detailed
VOFA+ 串口调试助手
(9) Attribute introspection
Swin Transformer【Backbone】