题解 | #高频数据类型#
高频数据类型
https://www.nowcoder.com/practice/687425f78096428baa58fbdcf024244a
高效、简洁、易读
1. javascript的基本类型有8种,number, string, boolean, bigint, symbol, undefined, null, object
2. 判断最高出现次数,只需要用一个max变量即可,不需要对数量进行排序
const _findMostType = array => { // 补全代码 let m = { 'number': 0, 'string': 0, 'boolean': 0, 'bigint': 0, 'symbol': 0, 'undefined': 0, 'null': 0, 'object': 0 }; let res = []; let max = 0; for (let v of array) { let t = typeof v; let isNull = t == 'object' && v === null; max = Math.max(max, ++m[isNull ? 'null' : t]); } for (let k in m) { if (m[k] == max) res.push(k); } res.push(max); return res; }