请补全JavaScript代码,要求找到参数数组中出现频次最高的数据类型,并且计算出出现的次数,要求以数组的形式返回。
注意:
1. 基本数据类型之外的任何引用数据类型皆为"object"
2. 当多种数据类型出现频次相同时将结果拼接在返回数组中,出现次数必须在数组的最后
__findMostType([0,0,'',''])
['number','string',2]或['string','number',2]
<script type="text/javascript"> const _findMostType = array => { // 补全代码 // 存放结果的数组 let arr = []; // 对象,数据类型为键名,键值为数据类型出现的次数 let o = {}; // 出现次数最多数据类型的次数 let maxnum = 0; // 遍历数组 array.forEach(item => { // 获取数据类型 -- 基本数据类型、object类型 let type = (typeof item) ? (typeof item) : 'object'; o[type] = o[type] ? o[type] + 1 : 1; maxnum = maxnum > o[type] ? maxnum: o[type]; }) // 生成返回数组 Object.keys(o).forEach(key => { if(o[key] === maxnum){ arr.push(key); } }) arr.push(maxnum); return arr; } </script>
代码并不复杂,原理是【对象key的唯一性】
将类型值作为对象的key,然后将出现的次数作为对象的value
let obj = {}, result = [], num = 0; array.map(item => { let type = typeof (item); obj[type] = (obj[type] || 0) + 1 num = Math.max(obj[type], num) }) for (let key in obj) { result = obj[key] == num ? [...result, key] : result } return [...result, num]
const _findMostType = array => { // 补全代码 let o = {}, arr = [], maxNum = 0; array.forEach(item => { let type = item === null ? 'null' : (typeof item === 'function' ? 'object' : (typeof item)); o[type] = o[type] ? o[type] + 1 : 1; maxNum = o[type] > maxNum ? o[type] : maxNum; }); Object.keys(o).forEach(key => { if(o[key] === maxNum){ arr.push(key); } }) arr.push(maxNum); return arr; }
const _findMostType = array => { // 补全代码 const res = {}; array.forEach((item) => { const type = typeof item === 'object' ? (item === null ? 'null' : 'object') : typeof item; res[type] = (res[type] ?? 0) + 1; }); return Object.entries(res).reduce( (res, cur) => { if (cur[1] > res.at(-1)) { res = [...cur]; } else if (cur[1] === res.at(-1)) { res.unshift(cur[0]); } return res; }, [0] ); }
const _findMostType = array => { // 补全代码 const stats = {}; // typeof null为object,需要单独处理null array.forEach(item => { let type = typeof item; if(item === null) { type = 'null'; } if(item instanceof Object) { type = 'object'; } if(!stats[type]) { stats[type] = 1; } else { stats[type] += 1; } }); const nums = Object.values(stats).sort((a,b) => b -a)[0]; const arr = Object.keys(stats).filter(type => stats[type] === nums).map(type => type); arr.push(nums); return arr; }
const _findMostType = array => { const count = {} let most = 0 for (let item of array) { let key = typeof item count[key] = (count[key] || 0) + 1 most = count[key] > most ? count[key] : most } // 因为 Object.values 导致不通过 // let most = Math.max(...Object.values(count)) let ans = [] for (let key in count) { if (count[key] === most) { ans.push(key) } } return [...ans, most] }
const _findMostType = array => { // 补全代码 let temp = {} // 存储数据类型和频次 let arr = [] // 存返回结果 let num = 0 // 存出现的最高次数 for (key of array) { let type = typeof key; temp[type] = temp[type] ? ++temp[type] : 1; num = Math.max(temp[type], num); } for (key in temp) { arr = temp[key] === num ? [...arr, key] : arr; } return [...arr, num] }
代码实现了统计每种类型的数量,最后根据给定情况分出现次数相同与出现次数不相同,返回对应结果,与给定题目不同的是,我将Object类型进行了统计。
const _findMostType = array => { // 补全代码 let result = array .map(item => typeof item) .sort() let judgeArray = [...new Set(result)] .map(item => result.lastIndexOf(item) - result.indexOf(item) + 1) if ([...new Set(judgeArray)].length === 1){ let sum = judgeArray.reduce((acc,cur) => acc = acc + cur,0) return [...new Set(result),sum] }else{ let max = judgeArray.reduce((acc,cur) => { if(acc<cur) acc = cur return acc },0) return [...new Set(result),max] } }
<!-- 请补全JavaScript代码,要求找到参数数组中出现频次最高的数据类型,并且计算出出现的次数,要求以数组的形式返回。 注意: 1. 基本数据类型之外的任何引用数据类型皆为"object" 2. 当多种数据类型出现频次相同时将结果拼接在返回数组中,出现次数必须在数组的最后 --> <!DOCTYPE html> <html> <head> <meta charset=utf-8> </head> <body> <script type="text/javascript"> const _findMostType = array => { // 补全代码 let kindArr = [] let maxNum = 0 let maxArr = [] let dataKind = { number: 0, string: 0, bool: 0, undefined: 0, null: 0, object: 0 } //循环判定每一个数据类型 for (let i in array) { if (typeof array[i] in dataKind) { (dataKind[typeof array[i]])++ } } //判断最大次数 for (let j in dataKind) { if (dataKind[j] >= maxNum) maxNum = dataKind[j] } for (let m in dataKind) { if (dataKind[m] === maxNum) { kindArr.push(m) } } kindArr.push(maxNum) console.log(kindArr); return kindArr } </script> </body> </html>
const _findMostType = array => { // 补全代码 function getType(val) { const type = typeof val; if (['string', 'number', 'boolean', 'symbol'].includes(type)) { return type; } if (val === null) { return 'null'; } if (val === undefined) { return 'undefined'; } return 'object'; } const typeMapper = {}; array.forEach(v => { const type = getType(v); if (typeMapper[type]) { typeMapper[type]++ } else { typeMapper[type] = 1; } }); return Object.entries(typeMapper).sort((pre, next) => { // 排序 return next[1] - pre[1]; }).filter((v, idx, arr) => { // 排除数量不等于第一个的元素 if (arr[0][1] !== v[1]) { return false; } return true; }).reduce((pre, cur, currentIndex, arr) => { // 如果是最后一个元素,则 concat, 否则只 concat 数组第一个元素 if (currentIndex === arr.length - 1) { return pre.concat(cur); } return pre.concat([cur[0]]); }, []); } console.log(_findMostType([1,"1",{},{},2,3,4,3,3,"","","","",""])); // [ 'number', 'string', 6 ] 应该符合要求
let map = new Map(); for(let a of array){ let type = typeof a; if(map.has(type)){ map.set(type,map.get(type)+1);//设置频次+1 }else{ map.set(type,1); } } let arr = []; function timeSort(a,b){return b-a;}//从大到小排序 let times = Array.from(map.values()).sort(timeSort); let n = 1; for(let i=0;i<times.length-1;i++){ if(times[i] == times[i+1]){ n++; }else{ break; } } let max = times[0]; if(n>1){ Array.from(map.keys()).forEach((item)=>{ if(map.get(item) == max){ arr.push(item); } }) } arr.push(times[0]); return arr;//最大值返回
const _findMostType = array => { // 补全代码 const typesArr = array.map((item) => item = typeof item); const count = typesArr.reduce((accumulator, currentValue) => { accumulator[currentValue] = (accumulator[currentValue] || 0) + 1; return accumulator; }, {}); const resultArr = [] for (const key in count) { if (Object.hasOwnProperty.call(count, key)) { const element = count[key]; if (resultArr.indexOf(element) < 0) { resultArr.unshift(key, count[key]) } else { resultArr.splice(resultArr.indexOf(element), 0, key) } } } return resultArr } // console.log(_findMostType([0, 0, 1, '', '', true, {}, null, undefined, false])) console.log(_findMostType([0, 0, '', '', true]))为什么不行啊 🆘
let obj={} array.forEach((items,index)=>{ if(!obj[typeof items]){ obj[typeof items]=1 }else{ obj[typeof items]++ } }) let arr=['',0]; for (const key in obj) { if(obj[key]>arr[arr.length-1]){ arr[arr.length-1]=obj[key] arr[0]=key if(arr.length>2){ for (let i = 2; i <= arr.length; i++) { arr.splice(i-1,1) } } }else if(obj[key]==arr[arr.length-1]){ arr.splice(0,0,key) arr[arr.length-1]=obj[key] arr[0]=key } } return arr }