Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
function that counts the number of character doubles entered by the user
Posted

Another method is to sort the characters in the string, then examine the sorted string.

Duplicate characters will be easy to find, since they will be next to each other.
 
Share this answer
 
Comments
Aescleal 22-May-12 13:47pm    
And that's particularly good if you're really limited with memory and someone gives you the data as a string you can sort.
Solution 1 is that you should keep track of how many times each character appears:

C#
int count[256]; // <-- assuming char is 8 bytes
for(i=0;i!=256;++i)
{
    count[i] = 0; // <-- set all counts to be zero
}
for(i=0;arr[i]!='\0';i++)
{
    count[arr[i]] = count[arr[i]] + 1;
    // now you can check if count is 1, and if so then do whatever
}
 
Share this answer
 
v2
Comments
Aescleal 22-May-12 13:01pm    
Gave this a 3 cause:

- My old bugbear, using built in arrays when a vector would work as well and be less source code - i.e. the declaration of the array and initialisation would only be one line. Thinking about it declaring and initialising the array should only be one line!

- I couldn't see what the second loop was up to then it dawned on me it was traversing an arbitrary array of characters and counting the number of each value. This was due to some pretty weird choices of variables and styles of coding (e.g. reusing i, using n=n+1 rather than n++ or ++n.
There are two questions to answer first:

- Which is more important, memory or execution time?
- How big is the character set you're processing?

If you've got plenty of memory available compared to the size of the size of the character set then a full frequency table (as in solution one) is probably the way to go. Please use C++ though and not some C relic[1]. Use:
C++
std::vector<int> frequency_table( 256 );
rather than an array. You could even be flash and use std::for_each to do the counting in not many lines from a stream or another collection.

If you're limited by memory OR you can't fit the full frequency table in the application's working set then you can use a sparse frequency table. Make a map of ints to ints:
C++
std::map<int,int> frequency_table;
and use it exactly as you would the table in the other solution. It'll be slower but if it's the difference between code working and not working I'll take slower any day.

And if you bury it all in a class you can template the lot on the collection you're using and change it if it's too slow/too big. What's not to like?

Oh, one final thing. If your code is meant to handle UTF-8 or UTF-16 remember that one C++ char/wchar_t may not be a full character in those representations.

[1] I use the word relic to refer to the practice of using C constructs in C++ programs. I'm not calling C a relic, it's not - it's still one of the most important languages on the planet.

Edit for editor fail - I should be more careful about pasting stuff
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900