Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for unique characters in a string

Tags:

c++

string

char

I'm trying to write a code that checks to see if a string is made up of unique characters. I do some data validation in the first few lines, and inputting code with these conditions (length = 1 or length > 36) works. When I input a string that does not meet the previously stated requirements, I attempted to compare each char of my string with every other char. Even for strings of unique characters, like the one in the following example, it returns that the string is not made up of unique characters.

string string = "abcdefghijklmnopqrstuvwxyz0123456789";
bool uniqueCharacters = false;

//if length is 1, automatically return that the string is made up of unique characters
if (string.length() == 1) {
    uniqueCharacters = true;
}

 //there are 26 letters and 10 numbers, so if a string is made up of more than 36 chars, there must be some overlap
if (string.length() > 36) {
    uniqueCharacters = false;
}

else if (string.length() > 1 && string.length() < 37) {
    for (int i = 0; i < string.length(); i++) {
        for (int j = 1; j < string.length(); j++) {
            if (string[i] == string[j]) {
                uniqueCharacters = false;
            }
            else {uniqueCharacters = true;}
        }
    }
}

if (uniqueCharacters == true) {
    cout << "This string contains all unique characters \n";
}
if (uniqueCharacters == false) {
    cout << "This string does not contain all unique characters \n";
}

I assume this is a logic error, but I cannot figure it out. Any ideas?

like image 241
lottie3 Avatar asked Jan 24 '26 06:01

lottie3


2 Answers

If you only care to check if a container contains only unique elements and you do not want to sort it then you can copy the data into a std::set. A std::set will only store unique items so once you populate the set if the size does not match the container you are using then you know there are duplicates.

Using the iterator constructor checking if a string contains only unique elements is as simple as

std::string line = "abcdefghijklmnopqrstuvwxyz0123456789";
std::set<char> checker(line.begin(), line.end());
if (checker.size() != line.size())
    std::cout << "contains duplicates!"; 
like image 61
NathanOliver Avatar answered Jan 26 '26 20:01

NathanOliver


bool uniqueCharacters = true; // <-- initialize to true then try to find if false

// We put the condition `uniqueCharacters` in the loop:
// no need to continue looping once we find a duplicate.
for (int i = 0; uniqueCharacters && i < string.length(); ++i) {
    for (int j = i+1; uniqueCharacters && j < string.length(); ++j) { // <-- start with j=i+1, to avoid checking a character versus itself
        if (string[i] == string[j]) {
            uniqueCharacters = false;
        }
    }
}

Finally, notice that there are std tools dedicated for duplicate management, such as std::unique and std::set<>...

And also notice that your algorithm is not the fastest possible for this task.

like image 27
A.S.H Avatar answered Jan 26 '26 21:01

A.S.H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!