Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Last active December 8, 2020 16:50
Show Gist options
  • Save BretCameron/e364b64792a06d3295830cdad56704ad to your computer and use it in GitHub Desktop.
Save BretCameron/e364b64792a06d3295830cdad56704ad to your computer and use it in GitHub Desktop.
Return true if two strings are anagrams of one another
const isAnagram = (str1, str2) => {
if (str1.length !== str2.length) return false;
const map = new Map();
for (let char of str1) {
const count = map.has(char) ? map.get(char) + 1 : 1;
map.set(char, count);
};
for (let char of str2) {
if (!map.has(char)) return false;
const count = map.get(char) - 1;
if (count === 0) {
map.delete(char);
continue;
};
map.set(char, count);
};
return map.size === 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment