Solved

C#: Searching duplicate letters in a string

Posted on 2015-02-09
7
192 Views
Last Modified: 2015-05-08
I wrote a function which finds a duplicate letter in a sentence (string) using one loop.
Example: “You are a genius” can loop only through maximum 16 characters to find a duplicate ‘a’.
Looks like a bit mask can be used to implement that.

Need the best solution.
 
Thank you in advance.
0
Comment
Question by:quasar_ee
[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
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40599709
Yes, a bit map can be used.  What did the function you wrote use?
Since you don't mention the duplicate ' ', I infer it doesn't count as a "letter"?
What characters count as letters?  Could 'Y' count as a duplicate of 'y'?  Could 'a' count as a duplicate of 'å'?
0
 

Author Comment

by:quasar_ee
ID: 40599793
Let's use only ASCII characters for now. Let's count 'Y' and 'y' as a duplicate. If it is a letter (ASCII code 65-132) and diff is 32 then it is duplicate. Tha's what I wrote. Need optimized solution, ideally using bitmask.
Please write a code snippet in C# if you'd like to help.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40599856
For 16 characters, I'd be more concerned with programmer optimization than machine optimization.
Unless this was to be run billions of times, in which case I might think about optimizing the data structure, and want to consider why you want to search for duplicate letters, how often you do it, what other operations must be supported on the data, etc.
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40599915
This question has been done here
http://www.dotnetperls.com/mask-optimization

You would just need to tweak the code to convert to lower case during processing eg

int index = value[i].ToLower() - 97;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40599982
On some machines it can be faster to use mask[ i ] than 1<<index,
assuming that you use it enough to be worth the cost of building the mask table.  The mask table can also integrate the case folding step.
A byte array can often be faster than a bit array to update, but can take longer to clear, so the tradeoff may depend on your usage pattern.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

737 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