题解 | #高频数据类型#
高频数据类型
https://www.nowcoder.com/practice/687425f78096428baa58fbdcf024244a
const _findMostType = (array) => { // 补全代码 let typaArr = []; array.forEach((item, index) => { let currentType = typeof item; console.log(currentType); typaArr.push(currentType); }); const countObj = typaArr.reduce((obj, newvalue) => { if (newvalue in obj) { obj[newvalue]++; } else { obj[newvalue] = 1; } return obj; }, {}); let max = 0; let arr = []; for (let i in countObj) { if (countObj[i] > max) { max = countObj[i]; arr = [i]; } else if (countObj[i] === max) { arr.push(i); } } if(arr.length>0){ arr.push(countObj[arr[0]]) } return arr };