题解 | 高频数据类型
高频数据类型
https://www.nowcoder.com/practice/687425f78096428baa58fbdcf024244a
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript">
const _findMostType = (array) => {
// 补全代码
let map = new Map();
let arr = [];
array.forEach((item) => {
let type = typeof item;
map.set(type, (map.get(type) || 0) + 1);
});
let maxNum = Math.max(...map.values());
for (let [key, value] of map) {
if (value === maxNum) {
arr.push(key);
}
}
arr.push(maxNum)
return JSON.stringify(arr);
};
console.log(
_findMostType([1, "1", {}, {}, 2, 3, 4, 3, 3, "", "", "", "", ""])
);
</script>
</body>
</html>
查看10道真题和解析