Isograms programming exercise
I am sergeant Sql Pavlov, and this are your orders:
Implement a function that determines if a given string have duplicate characters, or all the characters are unique.
Example: input: "aabc" => it have two 'a' so its not unique. input: "abcd" => there is no repeating characters.
As always there is many way to solve this coding problem, and the solution I will provide, will not be the fastest, or even will follow the best practices, because my goal is to choose a solution that will help beginners to develop better understanding of the programming languages. I use C# because is a C based language, and also is very modern widely used and somewhat easy to start with.
Scroll down for the solution. Good luck and have fun 🙂
An Isogram is a word with no repeating letters
Which make it perfect for naming our function
public static bool IsIsogram(string str) { //We are going to use ToLower() on "str" //in order to make all character lowercase str = str.ToLower(); //then we shall go through each character //and check its occurrences count foreach (var item in str) { //we will use "k" as a counter int k = 0; //now we will go through each character //of the string "str" for (int i = 0; i < str.Length; i++) { //and check if the characters occurs more the once if (item == str[i]) { k++; //and if it does, will know that it is not //an Isogram if (k==2) return false; } } } //but if it occurs only 1 time it is an Isogram return true; }