亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 中的經典字數統計算法

Javascript 中的經典字數統計算法

瀟瀟雨雨 2021-09-17 13:43:48
請... 伙計們,我哪里出錯了?經典的字數統計算法:給定一個字符串數組,返回一個 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 });});
查看完整描述

3 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

就目前而言,您的方法從根本上是錯誤的。您需要做的就是將數組中的每個字符串添加為一個屬性(如果還不是一個屬性),如果是,則增加其值。


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():


function wordCount(arr) {

  return arr.reduce(function(map, word) {

    if (word in map)

      map[word]++;

    else

      map[word] = 1;

    return map;

  }, {});

}


查看完整回答
反對 回復 2021-09-17
?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

最簡潔最簡單的方法是reduce

const wordCount = arr => arr.reduce((a, c) => ((a[c] = (a[c] || 0) + 1), a), {});


查看完整回答
反對 回復 2021-09-17
?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

試試這個(基于你的代碼):


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;

}


查看完整回答
反對 回復 2021-09-17
  • 3 回答
  • 0 關注
  • 264 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號