Given a string, write an algorithm to determine if all characters in the string are unique.

1 Answers
Answered by suresh

Algorithm: Determine if all characters in a string are unique

Algorithm: Determine if all characters in a string are unique

To determine if all characters in a given string are unique, you can create an algorithm that iterates through each character in the string and checks if it appears more than once. Here is a sample algorithm:

function areAllCharactersUnique(str) {
  let charSet = new Set();
  for (let char of str) {
    if (charSet.has(char)) {
      return false;
    } else {
      charSet.add(char);
    }
  }
  return true;
}

// Example usage
const str = "abcdefg";
const result = areAllCharactersUnique(str);
console.log(result); // Output: true

By using this algorithm, you can efficiently determine if all characters in a given string are unique.

Answer for Question: Given a string, write an algorithm to determine if all characters in the string are unique.