문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
메모
Set() constructor: 자료형에 관계 없이 원시 값과 객체 참조 모두 유일한 값을 저장할 수 있다.
// Use to remove duplicate elements from the array
const numbers = [2, 3, 4, 4, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 5, 32, 3, 4, 5];
console.log([...new Set(numbers)]);
// [2, 3, 4, 5, 6, 7, 32]
정답
function solution(my_string) {
return [...new Set(my_string)].join("");
}
// Set을 사용하지 않고 중복된 문자열 제거하기
function solution(my_string) {
let arr = [];
// my_string을 배열로 만들기
// 값이 arr에 없으면 추가하고 그렇지 않으면 스킵한다
[...my_string].map(x => (arr.includes(x) ? x : arr.push(x)));
return arr.join("");
}