DEV Community


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

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;
godcrampy profile image
Sahil Bondre Author

What a beautiful solution!