Solved

C#: Searching duplicate letters in a string

Posted on 2015-02-09
7
151 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
  • 3
7 Comments
 
LVL 84

Expert Comment

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

9 Experts available now in Live!

Get 1:1 Help Now