Skip to content

Instantly share code, notes, and snippets.

@hauntedhost
Last active December 20, 2015 23:59
Show Gist options
  • Save hauntedhost/6217011 to your computer and use it in GitHub Desktop.
Save hauntedhost/6217011 to your computer and use it in GitHub Desktop.
// Write a function to get the common letters out of two strings
// commonLetters('hello world', 'hello moon') => should return 'helo'
function commonLetters(str1, str2) {
var map = {},
common = '';
for (var i = 0; i < str1.length; i++) {
var letter = str1[i];
if (letter != ' ') map[letter] = true;
}
for (var i = 0; i < str2.length; i++) {
var letter = str2[i];
if (map[letter] && common.indexOf(letter) == -1) common += letter;
}
return common;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment