filter with Set

Isogram
Isogram in JavaScript
export function isIsogram(string) {
  let word = [...string.toLowerCase()].filter(
    (letter) => letter >= 'a' && letter <= 'z',
  );
  return new Set(word).size == word.length;
}

With this approach you will instantiate a Set of the used letters and compare its size with the filtered word length.

  • First, the lowercased input string is made into an Array of its characters using spread syntax.
  • The filter method is then called on the Array to filter out any character that is not a-z.
  • The letters that survive the filter are assigned to word.
  • A Set is initialized from the letters in word. The size of unique letters is compared with the length of the filtered word length.

If the number of unique letters in the Set of used letters is the same as the number of filtered letters, then the function returns true.

20th Nov 2024 · Found it useful?