This solution is generated by ChatGPT, correctness is not guaranteed.

AI solution for JavaScript Coding Question on BFE.dev
112. remove duplicate characters in a string

One possible solution is to use a set to keep track of seen characters, and a string variable to store the result. We iterate over the original string character by character, and add it to the result string only if it hasn't been seen before. We return the result string after finishing the iteration.

Here's the code:

function removeDuplicates(str) {
  const seen = new Set();
  let result = "";

  for (let i = 0; i < str.length; i++) {
    const char = str.charAt(i);
    if (!seen.has(char)) {
      seen.add(char);
      result += char;
    }
  }

  return result;
}

This function should have a time complexity of O(n), where n is the length of the input string, since we iterate over it once and the set operations take constant time on average. The space complexity is also O(n) in the worst case, if all characters are distinct and we need to store them all in the set.