ISourceCode

Make the frequent cases fast and the rare case correct

Determine if a string has all unique characters


class Program
{
    static void Main(string[] args)
    {
        string name = "hello";

        char[] characters = name.ToCharArray();

        Dictionary<char, int> dict = new Dictionary<char, int>();

        int count = 0;

        foreach (var x in characters)
        {
            if (dict.ContainsKey(x))
            {
                dict[x] = dict[x] + 1;
            }
            else
            {
                dict.Add(x, count+1);
            }
        }

        foreach (var kvp in dict)
        {
            Console.WriteLine(kvp.Key + " " + kvp.Value);

            if (kvp.Value > 1)
            {
                Console.WriteLine("String does not have all unique characters");
            }
        }

        Console.ReadKey();
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: