Skip to content

Instantly share code, notes, and snippets.

@shinout
Created October 4, 2011 13:04
Show Gist options
  • Save shinout/1261594 to your computer and use it in GitHub Desktop.
Save shinout/1261594 to your computer and use it in GitHub Desktop.
count tools (in topcoder)
/* http://topcoder.g.hatena.ne.jp/eller/20091207 */
function countTools(need) {
return need.join(' ').split(' ').reduce(function(sofar, v) {
if (!sofar.hash[v]) sofar.count++;
sofar.hash[v] = true;
return sofar;
}, {hash: {}, count: 0}).count;
}
console.log(countTools(
["SAW HAMMER SCREWDRIVER","SCREWDRIVER HAMMER KNIFE"]
), 4);
console.log(countTools(
["SAW"]
), 1);
console.log(countTools(
["SAW", "SAW"]
), 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment