Skip to content

Instantly share code, notes, and snippets.

@minmaxdata
Created August 13, 2018 23:58
Show Gist options
  • Select an option

  • Save minmaxdata/465db661065a0f11c0568bc6f19a137f to your computer and use it in GitHub Desktop.

Select an option

Save minmaxdata/465db661065a0f11c0568bc6f19a137f to your computer and use it in GitHub Desktop.
anagrams? created by minmaxdata - https://repl.it/@minmaxdata/anagrams
function anagram(first, second) {
let one = [...first];
let two = [...second];
let temp = {}
const len = one.length === two.length;
if (len) {
temp = one.reduce(function (letters, letter) {
if (letter in letters) {
letters[letter]++
} else {
letters[letter] = 1
}
return letters
}, {});
two.map(alpha => {
if (temp[alpha] > 0) {
temp[alpha]--;
}
if (temp[alpha] === 0) {
delete temp[alpha];
}
})
}
return Object.keys(temp).length === 0;
}
console.log('anagram', anagram('restful', 'fluster'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment