当前位置:网站首页>Implement a method to find the sum of the number k and m in the array
Implement a method to find the sum of the number k and m in the array
2022-07-26 03:10:00 【Two faces confused】
subject : Implement a method , Find the second in the array k And the first m Big numbers add up ;
Example :
let arr = [1,2,4,4,3,5], k = 2, m = 4
findTopSum(arr, k, m); // The first 2 The big number is 4, appear 2 Time , The first 4 The big one is 2, appear 1 Time , So the result is 10
( His writing , Of course, there may be more brief code )
function bigSum(arr, k, m) {
// Sort
arr = arr.sort(function (a, b) {
return b - a;
})
console.log(' Descending array ', arr)
// duplicate removal ( It's an object )
let newArr = new Set(arr);
// Converted to an array
let arr1 = [...newArr]
console.log(' Array after de duplication ', arr1);
let temp1 = arr1[k - 1];
let temp2 = arr1[m - 1];
let total1 = 0;// Calculate the number of identical elements
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(' The result of the calculation is ', result);
边栏推荐
- Safety margin of mass consumption
- [SQL] 自连接的用法
- How can users create data tables on Web pages and store them in the database
- Arthas download and startup
- Get twice the result with half the effort: learn the web performance test case design model
- snownlp库各功能及用法
- Image recognition (VI) | activation function
- 重装Win7系统如何进行?
- How to install with USB flash disk?
- An article allows you to understand the relevance of cloud native containerization
猜你喜欢

Opencv 以指定格式保存图片

Standardize your own debug process

Image recognition (VII) | what is the pooling layer? What's the effect?

(9) Attribute introspection

复制列表时踩过的坑:浅拷贝与深拷贝

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

How to design test cases according to the requirements of login testing?

(九)属性自省

Keyboardtraffic, a tool developed by myself to solve CTF USB keyboard traffic

Win11隐藏输入法状态栏方法
随机推荐
持续交付和DevOps是一对好基友
对于稳定性测试必需关注的26点
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
Programming example of STM32 state machine -- fully automatic washing machine (Part 1)
复制列表时踩过的坑:浅拷贝与深拷贝
File operation (I) -- File introduction and file opening and closing methods
Longest Substring Without Repeating Characters
Service gateway (zuul)
Parallelloopbody in opencv
多线程编程
STM - exti external interrupt learning notes
ENVI_ Idl: create HDF5 file and write data (take writing GeoTIFF file to HDF file as an example) + detailed parsing
Remember SQL optimization once
STM——EXTI外部中断学习笔记
STM32 - serial port learning notes (one byte, 16 bit data, string, array)
Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks
重装Win7系统如何进行?
Difference between soft link and hard link
My friend took 25koffer as soon as he learned automation test. When will my function test end?
小测(一)
