简体   繁体   English

在 javascript 或 vue 中查找重复次数最多和最少的数字

[英]Finding the most and least repeated numbers in javascript or vue

I have 500 numbers and I want to find the 5 most repeated numbers and the 5 least repeated numbers among them.我有 500 个号码,我想找出其中重复次数最多的 5 个号码和重复次数最少的 5 个号码。 I would love it if you could help me as an example how can I find it.如果你能帮助我作为一个例子,我会很高兴我怎么能找到它。

I am using vuejs.我正在使用 vuejs。

const data = [ "6", "30", "30", "21", "3", "7", "14", "17", "32", "25", "6", "3", "9", "27", "14", "33", "2", "29", "29", "30", "14", "6", "36", "5", "7", "6", "19", "3", "25", "26", "22", "27", "25", "2", "23", "4", "17", "32", "14", "30", "34", "26", "27", "33", "4", "5", "17", "4", "5", "33", "19", "12", "27", "26", "0", "17", "0", "6", "6", "35", "24", "21", "9", "0", "6", "0", "25", "33", "0", "0", "24", "11", "4", "7", "30", "16", "13", "36", "14", "32", "25", "23", "0", "1", "14", "1", "3", "11", "16", "10", "28", "33", "8", "8", "25", "24", "4", "32", "2", "14", "20", "23", "28", "18", "31", "23", "35", "7", "15", "34", "12", "9", "29", "30", "20", "12", "6", "22", "12", "22", "23", "11", "3", "20", "27", "32", "34", "30", "29", "20", "32", "33", "7", "28", "9", "13", "7", "35", "25", "3", "33", "8", "0", "19", "7", "7", "4", "20", "10", "23", "14", "10", "14", "18", "16", "7", "17", "35", "31", "14", "29", "29", "30", "32", "27", "23", "29", "31", "16", "1", "35", "11", "6", "1", "3", "17", "16", "0", "32", "5", "34", "6", "19", "31", "9", "12", "15", "2", "27", "30", "3", "27", "33", "31", "2", "14", "26", "17", "20", "19", "17", "12", "24", "34", "29", "26", "29", "5", "11", "26", "22", "17", "28", "3", "22", "17", "1", "26", "27", "20", "7", "31", "31", "27", "31", "36", "31", "35", "34", "6", "20", "36", "27", "2", "21", "8", "5", "16", "11", "26", "16", "10", "10", "31", "27", "11", "13", "29", "20", "6", "21", "34", "23", "1", "15", "18", "28", "23", "23", "28", "9", "12", "6", "33", "1", "32", "26", "29", "29", "9", "9", "13", "21", "31", "7", "15", "17", "2", "10", "2", "27", "33", "2", "24", "22", "33", "10", "21", "2", "32", "15", "19", "6", "19", "33", "19", "1", "4", "19", "14", "25", "34", "15", "34", "7", "1", "35", "14", "31", "1", "3", "34", "31", "18", "15", "27", "17", "15", "33", "31", "33", "13", "26", "19", "8", "9", "30", "16", "22", "35", "1", "32", "22", "14", "18", "0", "22", "2", "11", "27", "33", "10", "23", "14", "29", "20", "1", "0", "22", "24", "12", "31", "8", "13", "2", "17", "11", "9", "3", "25", "33", "4", "12", "6", "33", "31", "1", "30", "28", "29", "16", "15", "31", "32", "30", "36", "19", "36", "20", "5", "4", "18", "6", "31", "9", "28", "6", "31", "2", "25", "36", "15", "34", "26", "5", "6", "6", "28", "12", "20", "6", "12", "9", "0", "10", "10", "9", "33", "21", "24", "1", "30", "9", "2", "36", "9", "19", "19", "9", "12", "9", "4", "13", "25", "19", "5", "2", "24", "1", "26", "10", "9", "26", "35", "0", "33", "20", "15", "23", "31", "22", "15", "9", "0", "7", "18", "20", "30", "34", "2", "32", "21", "24", "8", "13", "25", "9", "8", "5", "1", "11", "2", "25", "12", "35", "7", "31", "6", "26", "25", "1", "31", "32", "22", "15", "6", "6", "27", "26", "32", "6", "26", "30", "7", "19", "15", "5", "4", "21", "3", "24", "10", "25", "4", "7", "15", "28", "33", "35", "14", "6", "30", "30", "21", "3", "7", "14", "17", "32", "25", "6", "3" ]

Vanilla JS香草JS

 const data = [ "6", "30", "30", "21", "3", "7", "14", "17", "32", "25", "6", "3", "9", "27", "14", "33", "2", "29", "29", "30", "14", "6", "36", "5", "7", "6", "19", "3", "25", "26", "22", "27", "25", "2", "23", "4", "17", "32", "14", "30", "34", "26", "27", "33", "4", "5", "17", "4", "5", "33", "19", "12", "27", "26", "0", "17", "0", "6", "6", "35", "24", "21", "9", "0", "6", "0", "25", "33", "0", "0", "24", "11", "4", "7", "30", "16", "13", "36", "14", "32", "25", "23", "0", "1", "14", "1", "3", "11", "16", "10", "28", "33", "8", "8", "25", "24", "4", "32", "2", "14", "20", "23", "28", "18", "31", "23", "35", "7", "15", "34", "12", "9", "29", "30", "20", "12", "6", "22", "12", "22", "23", "11", "3", "20", "27", "32", "34", "30", "29", "20", "32", "33", "7", "28", "9", "13", "7", "35", "25", "3", "33", "8", "0", "19", "7", "7", "4", "20", "10", "23", "14", "10", "14", "18", "16", "7", "17", "35", "31", "14", "29", "29", "30", "32", "27", "23", "29", "31", "16", "1", "35", "11", "6", "1", "3", "17", "16", "0", "32", "5", "34", "6", "19", "31", "9", "12", "15", "2", "27", "30", "3", "27", "33", "31", "2", "14", "26", "17", "20", "19", "17", "12", "24", "34", "29", "26", "29", "5", "11", "26", "22", "17", "28", "3", "22", "17", "1", "26", "27", "20", "7", "31", "31", "27", "31", "36", "31", "35", "34", "6", "20", "36", "27", "2", "21", "8", "5", "16", "11", "26", "16", "10", "10", "31", "27", "11", "13", "29", "20", "6", "21", "34", "23", "1", "15", "18", "28", "23", "23", "28", "9", "12", "6", "33", "1", "32", "26", "29", "29", "9", "9", "13", "21", "31", "7", "15", "17", "2", "10", "2", "27", "33", "2", "24", "22", "33", "10", "21", "2", "32", "15", "19", "6", "19", "33", "19", "1", "4", "19", "14", "25", "34", "15", "34", "7", "1", "35", "14", "31", "1", "3", "34", "31", "18", "15", "27", "17", "15", "33", "31", "33", "13", "26", "19", "8", "9", "30", "16", "22", "35", "1", "32", "22", "14", "18", "0", "22", "2", "11", "27", "33", "10", "23", "14", "29", "20", "1", "0", "22", "24", "12", "31", "8", "13", "2", "17", "11", "9", "3", "25", "33", "4", "12", "6", "33", "31", "1", "30", "28", "29", "16", "15", "31", "32", "30", "36", "19", "36", "20", "5", "4", "18", "6", "31", "9", "28", "6", "31", "2", "25", "36", "15", "34", "26", "5", "6", "6", "28", "12", "20", "6", "12", "9", "0", "10", "10", "9", "33", "21", "24", "1", "30", "9", "2", "36", "9", "19", "19", "9", "12", "9", "4", "13", "25", "19", "5", "2", "24", "1", "26", "10", "9", "26", "35", "0", "33", "20", "15", "23", "31", "22", "15", "9", "0", "7", "18", "20", "30", "34", "2", "32", "21", "24", "8", "13", "25", "9", "8", "5", "1", "11", "2", "25", "12", "35", "7", "31", "6", "26", "25", "1", "31", "32", "22", "15", "6", "6", "27", "26", "32", "6", "26", "30", "7", "19", "15", "5", "4", "21", "3", "24", "10", "25", "4", "7", "15", "28", "33", "35", "14", "6", "30", "30", "21", "3", "7", "14", "17", "32", "25", "6", "3" ] /* * Create a Map to keep track of the number of occurrences for each element: * element -> count */ let map = new Map(); for (let i=0; i<data.length; i++) { let count = map.has(data[i]) ? map.get(data[i])+1 : 1; map.set(data[i], count) } /* Convert the Map into an array of arrays and sort by the 'count'*/ sorted = [...map].sort((first, second) => first[1] - second[1]); /* first five element are the ones that occur the least */ let fiveLeast = sorted.slice(0,5); /* last five are the ones that occur the most */ let fiveMost = sorted.slice(-5); /* drop the .map(e => e[0]) part if you want to see the number of times the respective elements occur */ console.log("These five occur the least: [" + fiveLeast.map(e => e[0]) + "]"); console.log("These five occur the most: [" + fiveMost.map(e => e[0]) + "]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM