Javascript 中的经典字数统计算法

请... 伙计们,我哪里出错了?


经典的字数统计算法:给定一个字符串数组,返回一个 Map ,每个不同的字符串都有一个键,值是该字符串在数组中出现的次数。


wordCount(["a", "b", "a", "c", "b"]) → {"a": 2, "b": 2, "c": 1}


wordCount(["c", "b", "a"]) → {"a": 1, "b": 1, "c": 1}


wordCount(["c", "c", "c", "c"]) → {"c": 4}


到目前为止我的代码


function wordCount(arrayOfStrings) {

    const map = {};


    const arr = arrayOfStrings;


    for (let i = 0; i < arr.length; i++) {


        let arr2 = arr.charAt(i);


        if (arr.indexOf(arr2) === arr.lastIndexOf(arr2)) {

            map.push({

                arr: arr2

            });

        }

    }

}


wordCount(["a", "b", "a", "c", "b"])

下面是我要通过的测试


test(`Expect the wordCount of ["one", "fish", "two", "fish", "red", "fish", "blue", "fish"] to equal {one: 1, fish: 4, two: 1, red: 1, blue: 1}`, () => {

expect(wordCount([ 'one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish' ])).toEqual({ one: 1, fish: 4, two: 1, red: 1, blue: 1 });

});


test(`Expect the wordCount of ["str", "hell", "str", "str"] to equal {str: 3, hell: 1}`, () => {

expect(wordCount([ 'str', 'hell', 'str', 'str' ])).toEqual({ str: 3, hell: 1 });

});


test(`Expect the wordCount of ["a", "b", "a", "c", "b"] to equal {"a": 2, "b": 2, "c": 1}`, () => {

expect(wordCount([ 'a', 'b', 'a', 'c', 'b' ])).toEqual({ a: 2, b: 2, c: 1 });

});


test(`Expect the wordCount of [1, "chair", "cane", "chair"] to equal {1: 1, chair: 2, cane: 1}`, () => {

expect(wordCount([ 1, 'chair', 'cane', 'chair' ])).toEqual({ 1: 1, chair: 2, cane: 1 });

});


test(`Expect the wordCount of ["ch", "chair", "cane", "chair", "ai", "ir"] to equal { ch: 1, chair: 2, cane: 1, ai: 1, ir: 1 }`, () => {

expect(wordCount([ 'ch', 'chair', 'cane', 'chair', 'ai', 'ir' ])).toEqual({ ch: 1, chair: 2, cane: 1, ai: 1, ir: 1 });

});


潇潇雨雨
浏览 233回答 3
3回答

慕盖茨4494581

就目前而言,您的方法从根本上是错误的。您需要做的就是将数组中的每个字符串添加为一个属性(如果还不是一个属性),如果是,则增加其值。function wordCount(arrayOfStrings) {&nbsp; &nbsp; const map = {};&nbsp; &nbsp; for (let i = 0; i < arrayOfStrings.length; ++i) {&nbsp; &nbsp; &nbsp; if (arrayOfStrings[i] in map)&nbsp; &nbsp; &nbsp; &nbsp; map[arrayOfStrings[i]]++;&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; map[arrayOfStrings[i]] = 1;&nbsp; &nbsp; }&nbsp; &nbsp; return map;}该代码检查数组中的每个字符串以查看它是否已经是正在构建的地图(一个普通对象)的一个属性。如果是,则增加该值;如果不是,则创建一个新属性并将其初始化为 1。使用会更整洁一些.reduce():function wordCount(arr) {&nbsp; return arr.reduce(function(map, word) {&nbsp; &nbsp; if (word in map)&nbsp; &nbsp; &nbsp; map[word]++;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; map[word] = 1;&nbsp; &nbsp; return map;&nbsp; }, {});}

慕容708150

最简洁最简单的方法是reduce:const&nbsp;wordCount&nbsp;=&nbsp;arr&nbsp;=>&nbsp;arr.reduce((a,&nbsp;c)&nbsp;=>&nbsp;((a[c]&nbsp;=&nbsp;(a[c]&nbsp;||&nbsp;0)&nbsp;+&nbsp;1),&nbsp;a),&nbsp;{});

翻过高山走不出你

试试这个(基于你的代码):function wordCount(arrayOfStrings) {&nbsp; &nbsp; const map = {};&nbsp; &nbsp; const arr = arrayOfStrings;&nbsp; &nbsp; for (let i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; map[arr[i]] = (map[arr[i]] || 0) +1;&nbsp; &nbsp; }&nbsp; &nbsp; return map;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript