Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

C#: Searching duplicate letters in a string

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
quasar_ee
Asked:
quasar_ee
  • 3
1 Solution
 
ozoCommented:
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
 
quasar_eeAuthor Commented:
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
 
ozoCommented:
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
 
Michael FowlerSolutions ConsultantCommented:
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
 
ozoCommented:
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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now