Created
October 4, 2011 13:04
-
-
Save shinout/1261594 to your computer and use it in GitHub Desktop.
count tools (in topcoder)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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