Created
February 27, 2024 20:28
-
-
Save agustinzamar/6b78bf15d97915535682ecf0913b3dbe to your computer and use it in GitHub Desktop.
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
const testCases = [ | |
'123123123', | |
'abc123', | |
'aaabbbccc', | |
'12345678' | |
] | |
function isUnique(string = ''){ | |
return (new Set(string)).size === string.length | |
} | |
function isUniqueBonus(string = ''){ | |
let unique = true; | |
const existingChars = []; | |
Array.from(string).forEach(char => { | |
if(existingChars.includes(char)){ | |
unique = false; | |
return; | |
} | |
existingChars.push(char) | |
}) | |
return unique; | |
} | |
// Tests | |
console.log('----- isUnique -----') | |
console.log(isUnique(testCases[0]) === false ? 'pass' : 'fails') | |
console.log(isUnique(testCases[1]) === true ? 'pass' : 'fails') | |
console.log(isUnique(testCases[2]) === false ? 'pass' : 'fails') | |
console.log(isUnique(testCases[3]) === true ? 'pass' : 'fails') | |
console.log('----- isUniqueBonus -----') | |
console.log(isUniqueBonus(testCases[0]) === false ? 'pass' : 'fails') | |
console.log(isUniqueBonus(testCases[1]) === true ? 'pass' : 'fails') | |
console.log(isUniqueBonus(testCases[2]) === false ? 'pass' : 'fails') | |
console.log(isUniqueBonus(testCases[3]) === true ? 'pass' : 'fails') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment