Skip to content

Instantly share code, notes, and snippets.

@agustinzamar
Created February 27, 2024 20:28
Show Gist options
  • Save agustinzamar/6b78bf15d97915535682ecf0913b3dbe to your computer and use it in GitHub Desktop.
Save agustinzamar/6b78bf15d97915535682ecf0913b3dbe to your computer and use it in GitHub Desktop.
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