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

Posted on 2013-12-12
Last Modified: 2013-12-12
Can any one help with this please?
Question by:Techsavy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

virtuadept earned 500 total points
ID: 39714881
This solution was found here:

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];

Open in new window


Author Closing Comment

ID: 39714929
Very Easy to Follow

Expert Comment

ID: 39715002
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!

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.Net problem with GridView using filters and checkboxes on lines 9 42
Building an object from several other objects 4 40
Datagridview column resizing 8 28
Example code 13 45
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question