Skip to content

Instantly share code, notes, and snippets.

@lukem512
Created December 12, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukem512/56c8fcd2501933127b362b6e94dc27f9 to your computer and use it in GitHub Desktop.
Save lukem512/56c8fcd2501933127b362b6e94dc27f9 to your computer and use it in GitHub Desktop.
// Sort the strings alphabetically, then sort the strings by length
// Naively compare each string to the others and try to find anagrams.
function hasAnagram(strings) {
const sorted = strings.map(s => s.split("").sort().join("")).sort();
return sorted.some((s, i) => {
const sliced = sorted.slice(0, i).concat(sorted.slice(i+1));
return sliced.find(s2 => s == s2);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment