Distinct

Isogram
Isogram in C#
using System.Linq;

public static class Isogram
{
    public static bool IsIsogram(string word)
    {
        var lowerLetters = word.ToLower().Where(char.IsLetter).ToList();
        return lowerLetters.Distinct().Count() == lowerLetters.Count;
    }
}

The steps for this solution are

  • ToLower produces a new string with its characters changed to lower case. That string is chained to Where.
  • Where uses IsLetter to filter the string so only Unicode letters get put into a List. So no hyphens nor apostrophes (nor anything else that is not a letter) will make it into the List.
  • Distinct uses Count to get the number of unique letters in the lowerLetters List.
  • The function returns whether the number of distinct letters is the same as the number of all letters.

A string is an isogram if its number of distinct letters is the same as the number of all its letters.

  • The word Bravo is an isogram because it has five distinct letters and five letters total.
  • The word Alpha is not an isogram because a is considered to repeat A, so it has only four distinct letters but five letters total.
11th Dec 2024 · Found it useful?