?
Solved

C#: Searching duplicate letters in a string

Posted on 2015-02-09
7
Medium Priority
?
214 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:
Michael Fowler earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

719 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