[프로그래머스 | 자바스크립트] 한 번만 등장한 문자

솔루션

const solution = (s) => {
  const count = [...s].reduce(
    (a, c) => (a[c] ? { ...a, [c]: a[c] + 1 } : { ...a, [c]: 1 }),
    {}
  );
  console.log(count);
  const answer = Object.keys(count)
    .filter((key) => count[key] === 1)
    .sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
    .join("");
  console.log(answer);

  return answer;
};

 

솔루션

const solution = (s) => {
  let answer = [];
  for (const v of s) {
    if (s.indexOf(v) === s.lastIndexOf(v)) {
      answer.push(v);
    }
  }
  answer.sort().join("");
  console.log(answer);

  return answer;
};