Skip to content

Instantly share code, notes, and snippets.

@SK-CSE
Forked from bradoyler/mapReduce-WordCounter.js
Created July 17, 2017 06:12
Show Gist options
  • Save SK-CSE/65ffe9114de44990c2e3608a4d1bb69e to your computer and use it in GitHub Desktop.
Save SK-CSE/65ffe9114de44990c2e3608a4d1bb69e to your computer and use it in GitHub Desktop.
Using Map-Reduce in Javascript: counting words in a string.
// for counting words in a string
var words="Hi there and hello there. Welcome and hello there.";
var wordcnt = words.replace(/[^\w\s]/g, "").split(/\s+/).reduce(function(map, word){
map[word] = (map[word]||0)+1;
return map;
}, Object.create(null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment