列表

详情


JS38. 高频数据类型

描述

请补全JavaScript代码,要求找到参数数组中出现频次最高的数据类型,并且计算出出现的次数,要求以数组的形式返回。
注意:
1. 基本数据类型之外的任何引用数据类型皆为"object"
2. 当多种数据类型出现频次相同时将结果拼接在返回数组中,出现次数必须在数组的最后

示例1

输入:

__findMostType([0,0,'',''])

输出:

['number','string',2]或['string','number',2]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 77884KB, 提交时间: 2022-01-25

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _findMostType = array => {\n                // 补全代码\n                //第一步:先用对象o保存数组里面的类型以及对应出现的频率,maxNum保存最大频率数\n                let o={}\n                let maxNum=0\n                let arr=[]\n                array.forEach(item=>{\n                    let type= item===null ? 'null' : (typeof item ==='function' ? 'object' : (typeof item))\n                    o[type]=o[type] ? o[type]+1 : 1\n                    maxNum=maxNum>o[type] ? maxNum : o[type]\n                })\n                //第二步:在o里面找到最高频率的对象存在arr里\n                Object.keys(o).forEach(key=>{\n                    if(o[key]==maxNum){\n                        arr.push(key)\n                    }\n                })\n                arr.push(maxNum)\n                return arr\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77772KB, 提交时间: 2021-12-20

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _findMostType = array => {\n    // 补全代码\n    let obj = {}\n    let maxCnt = 0\n    for (let item of array) {\n        let type = typeof item\n        if (!obj[type]) {obj[type] = 0}\n        ++obj[type]\n        if(obj[type]>maxCnt){maxCnt = obj[type]}\n    }\n    let res = []\n    for(let key in obj){\n        if(obj[key]==maxCnt){res.push(key)}\n    }\n    res.push(maxCnt)\n    return res\n}\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77760KB, 提交时间: 2021-12-07

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _findMostType = array => {\n                // 补全代码\n                let obj = {}\n                let arr = []\n                let num = 0\n                for (item of array) {\n                    let type = typeof item\n                    if (obj[type]) obj[type]++\n                    else obj[type] = 1\n                    num = obj[type] > num ? obj[type] : num\n                }\n                for (key in obj) {\n                    arr = obj[key] === num ? [...arr, key] : arr\n                }\n                return [...arr, num]\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1745ms, 内存消耗: 77840KB, 提交时间: 2022-01-25

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _findMostType = array => {\n                // 补全代码\n                  let typeArr = []\n                  let AlltypeObject = []\n                  array.forEach(element=>{\n                        let objType={\n                            type:typeof element,\n                            count:1\n                        }\n                    if(typeArr.indexOf(typeof element) === -1){\n                                typeArr.push(typeof element)\n                            AlltypeObject.push(objType)\n                    }else{\n                            AlltypeObject.forEach(elementObject =>{\n                                if(elementObject.type == typeof element){\n                                    elementObject.count++\n                                }\n                            })\n                    } \n                  })\n                let maxCount = Math.max(...AlltypeObject.map(element=>{return element.count}));\n                let maxArray = AlltypeObject.filter(element=>{return maxCount==element.count})\n                  return [...maxArray.map(element=>{return element.type}),maxCount]  \n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 77772KB, 提交时间: 2022-01-05

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _findMostType = array => {\n                // 补全代码\n                let data={};let maxNum=0;let arr=[];\n                for(let i=0;i<array.length;i++){\n                    var t=array[i]===null?'null':(typeof item==='function'?'object': (typeof array[i]))\n                    data[t]=data[t]?data[t]+1:1;\n                    maxNum=data[t]>maxNum?data[t]:maxNum;\n                }\n               for (let key of Object.keys(data)) \n               {\n                   if(data[key]===maxNum){\n                       arr.push(key);\n                   }\n               }\n                arr.push(maxNum);\n                return arr;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题