Solved

C#: Searching duplicate letters in a string

Posted on 2015-02-09
7
181 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add different cell to otherwise similiar row 4 45
Can I command line build a C# application? 6 40
bound data table problem 2 32
Indy 10 not Receiving UDP broadcast 3 21
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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

789 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