filter with distinct

Izogram
Izogram a(z) Scala kurzusban
import java.util.Locale

object Isogram:
  def isIsogram(s: String): Boolean =
    val letters = s.toLowerCase(Locale.ROOT).filter(_.isLetter)
    letters.distinct.length == letters.length

This approach starts by lowercasing the string with toLowerCase, so that A and a count as the same letter.

The filter() method keeps only the characters for which the function passed to it returns true. _.isLetter is a short way to write the lambda c => c.isLetter. isLetter is true for a letter and false for a space, a hyphen or a digit, so those are dropped. The result is kept in the value letters, because it is needed twice.

The distinct method removes the repeated characters, keeping the first occurrence of each. If no letter is repeated, there is nothing to remove, and the result is as long as letters. If any letter is repeated, the result is shorter. So comparing the two lengths answers the question.

Since the comparison is the last expression of the method, its value is returned without needing the return keyword.

2026. november 22. · Hasznosnak találtad?