Skip to content

Instantly share code, notes, and snippets.

@ilmsg
Forked from bradoyler/mapReduce-WordCounter.js
Created April 7, 2017 14:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilmsg/2bb4c37c5f14e5622eeb53b723b56796 to your computer and use it in GitHub Desktop.
Save ilmsg/2bb4c37c5f14e5622eeb53b723b56796 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