- By following c# example we can verify the given string has the all unique characters or string has duplicate characters.
class StringUnique
{
static bool uniqueCharacters(String str)
{
// If at any time we encounter 2
// same characters, return false
for (int i = 0; i < str.Length; i++)
for (int j = i + 1; j < str.Length; j++)
if (str[i] == str[j])
return false;
// If no duplicate characters
// encountered, return true
return true;
}
public static void Main()
{
string input = "abcdef";
if (uniqueCharacters(input) == true)
Console.WriteLine("The String " + input
+ " has all unique characters");
else
Console.WriteLine("The String " + input
+ " has duplicate characters");
Console.ReadLine();
}
}
No comments