I need assistance with a code that checks if all characters in a string are unique. I've noticed that the current code always returns true, which seems to be due to the false
output of the if
condition unless the first two characters in the sorted list are identical.
Could someone please provide some guidance on this?
function checkIfStringIsUnique(str) {
var chars = str.split('');
var sortedChars = chars.sort();
console.log(sortedChars);
console.log(sortedChars.length);
for (i = 0; i < sortedChars.length; i++) {
if (sortedChars[i] === sortedChars[i + 1]) {
return false;
}
return true;
}
}