Created
June 8, 2023 15:28
-
-
Save cferdinandi/2de9fee0cdc83e149d77534e341506b8 to your computer and use it in GitHub Desktop.
https://gomakethings.com/how-to-test-if-all-characters-in-a-string-a-unique-with-vanilla-javascript/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>isUniqueString()</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
</head> | |
<body> | |
<script> | |
let str1 = 'Hello world!'; | |
let str2 = 'cute dog!'; | |
function isUniqueString (str) { | |
str = str.toLowerCase(); | |
for (let i = 0; i < str.length; i++) { | |
if (str.indexOf(str[i]) !== i) return false; | |
} | |
return true; | |
} | |
function isUniqueString2 (str) { | |
let orig = str.toLowerCase().split(''); | |
let uniq = orig.filter(function (char, index) { | |
return orig.indexOf(char) === index; | |
}); | |
return orig.length === uniq.length; | |
} | |
function isUniqueString3 (str) { | |
let orig = str.toLowerCase().split(''); | |
let uniq = Array.from(new Set(orig)); | |
return orig.length === uniq.length; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment