Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-12
3
Medium Priority
?
748 Views
Last Modified: 2013-12-12
Can any one help with this please?
0
Comment
Question by:Techsavy
[X]
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
3 Comments
 
LVL 8

Accepted Solution

by:
virtuadept earned 2000 total points
ID: 39714881
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
 

Author Closing Comment

by:Techsavy
ID: 39714929
Very Easy to Follow
0
 
LVL 8

Expert Comment

by:virtuadept
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!
0

Featured Post

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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