알고리즘
[프로그래머스] 한 번만 등장한 문자
limew
2023. 7. 27. 16:55
https://school.programmers.co.kr/learn/courses/30/lessons/120896
방법1
문자열.lastIndexOf()
function solution(s) {
const arr = [];
for (const c of s) {
if (s.indexOf(c) === s.lastIndexOf(c)) {
arr.push(c);
}
}
return arr.sort().join('');
}
방법2
function solution(s) {
var answer = '';
const obj = {};
for (const element of s) {
obj[element] = (obj[element] || 0) +1;
}
// 1번만 등장하는 문자만 솎아내기
const once = Object.entries(obj).filter(o => o[1] === 1).map(e => e[0]);
// 1번만 등장하는 문자들을 사전 순으로 정렬
return once.sort().join('');
}