DEV Community

AsadullohRuziev
AsadullohRuziev

Posted on

Is Unique?

We'll discuss three ways to determine if all characters in a string are unique. Each has benefits and drawbacks regarding time and space complexity.

Input: String
Output: Boolean

Examples

isUnique('abcdef'); // -> true
isUnique('89%df#$^a&x'); // -> true
isUnique('abcAdef'); // -> true
isUnique('abcaef'); // -> false

*Solution 1
*

`
function isUnique(str) {
for(let i = 0; i < str.length; i++) {
if(str.lastIndexOf(str[i]) !== i) {
return false;
}
}

return true;
Enter fullscreen mode Exit fullscreen mode

}

`

Top comments (0)