首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript中的经典字数算法

Javascript中的经典字数算法
EN

Stack Overflow用户
提问于 2019-08-20 00:28:15
回答 3查看 2.1K关注 0票数 0

请..。伙计们,我哪里出问题了?

经典的字数算法:给定一个字符串数组,为每个不同的字符串返回一个带键的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":4}

到目前为止我的代码

代码语言:javascript
复制
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"])

及以下是我要通过的考试

代码语言:javascript
复制
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 });
});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-08-20 00:36:36

从目前来看,你的做法是完全错误的。您所需要做的就是将数组中的每个字符串添加为一个属性(如果它还不是一个属性),如果是的话,则增加它的值。

代码语言:javascript
复制
function wordCount(arrayOfStrings) {
    const map = {};
    for (let i = 0; i < arrayOfStrings.length; ++i) {
      if (arrayOfStrings[i] in map)
        map[arrayOfStrings[i]]++;
      else
        map[arrayOfStrings[i]] = 1;
    }

    return map;
}

该代码检查数组中的每个字符串,看看它是否已经是正在构造的映射(一个普通对象)的属性。如果是,则值将递增;如果不是,则创建一个新属性并将其初始化为1。

使用.reduce()会更整洁一些

代码语言:javascript
复制
function wordCount(arr) {
  return arr.reduce(function(map, word) {
    if (word in map)
      map[word]++;
    else
      map[word] = 1;
    return map;
  }, {});
}
票数 5
EN

Stack Overflow用户

发布于 2019-08-20 00:46:44

最简洁和最简单的方法是reduce

代码语言:javascript
复制
const wordCount = arr => arr.reduce((a, c) => ((a[c] = (a[c] || 0) + 1), a), {});
票数 1
EN

Stack Overflow用户

发布于 2019-08-20 00:39:55

尝试这个(基于您的代码):

代码语言:javascript
复制
function wordCount(arrayOfStrings) {
    const map = {};
    const arr = arrayOfStrings;

    for (let i = 0; i < arr.length; i++) {
        map[arr[i]] = (map[arr[i]] || 0) +1;
    }
    return map;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57565345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档