DEV Community

loading...

Discussion on: Interview Prep #1: Does String has Unique Characters

Collapse
witaylor profile image
Will Taylor

Here's a JavaScript solution. I split the string into a list of characters, and then create a new set from it. A set can only contain unique elements; so if the character list and the set have different lengths, then the original string must have contained duplicated letters.

const isStringUnique = string => {
    const charList = string.split('');
    const charSet = new Set(charList);

    return charList.length === charSet.size;
}
Collapse
godcrampy profile image
Sahil Bondre Author

What a beautiful solution!