Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Created June 8, 2023 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/2de9fee0cdc83e149d77534e341506b8 to your computer and use it in GitHub Desktop.
Save cferdinandi/2de9fee0cdc83e149d77534e341506b8 to your computer and use it in GitHub Desktop.
<!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