C# Algorithm: Given a string, find the first non-repeating character in it

Can any one help with this please?
TechsavyAsked:
Who is Participating?
 
virtuadeptCommented:
This solution was found here:
stackoverflow.com/questions/3994644/c-sharp-logic-to-get-the-first-non-repeatingdistinct-character-from-the-string

LINQ version:

using System.Linq;
string str = "abcabda";
char result = str.FirstOrDefault(ch => str.IndexOf(ch) == str.LastIndexOf(ch));

Open in new window


Non LINQ version:

 
  for (int index = 0; index < str.Length; index++)
    {
        if (str.LastIndexOf(str[index]) == str.IndexOf(str[index]))
        {
            result = str[index];
            break;
        }
    }

Open in new window

0
 
TechsavyAuthor Commented:
Very Easy to Follow
0
 
virtuadeptCommented:
I like the one-liner LINQ solution if you have a recent C#.NET, but for older versions of C# you may need to use the looping solution. Enjoy!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.