Solved

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

Posted on 2013-12-12
3
585 Views
Last Modified: 2013-12-12
Can any one help with this please?
0
Comment
Question by:Techsavy
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
virtuadept earned 500 total points
Comment Utility
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
Comment Utility
Very Easy to Follow
0
 
LVL 8

Expert Comment

by:virtuadept
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now