-
[Programmers] JavaScript 알고리즘 | Lv.0 중복된 문자 제거► JS Algorithm/Programmers 2024. 1. 10. 20:20반응형
🔒 문제 설명
문자열 my_string이 매개변수로 주어집니다. my_string에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열을 return하도록 solution 함수를 완성해주세요.
🔒 제한사항
- 1 ≤ my_string ≤ 110
- my_string은 대문자, 소문자, 공백으로 구성되어 있습니다.
- 대문자와 소문자를 구분합니다.
- 공백(" ")도 하나의 문자로 구분합니다.
- 중복된 문자 중 가장 앞에 있는 문자를 남깁니다.
🔒 입출력 예
my_string result "people" "peol" "We are the world" "We arthwold"
🔒 입출력 예 설명
입출력 예 #1
- "people"에서 중복된 문자 "p"와 "e"을 제거한 "peol"을 return합니다.
입출력 예 #2
- "We are the world"에서 중복된 문자 "e", " ", "r" 들을 제거한 "We arthwold"을 return합니다.
🔐 solution of mine
for문
if문
Array.includes()
Array.push()
Array.join()
const solution = (my_string, answer = []) => { for (let i = 0; i < my_string.length; i++) { if (!answer.includes(my_string[i])) { answer.push(my_string[i]); } } return console.log(answer.join("")); }; solution("people"); // expected output: "peol" solution("We are the world"); // expected output: "We arthwold"
🔐 solution of others
new Set()
Array.join()
const solution = (my_string) => console.log([...new Set(my_string)].join("")); solution("people"); // expected output: "peol" solution("We are the world"); // expected output: "We arthwold"
new Set() 은 중복값을 없애준다.
🔐 solution of others
Array.indexOf()
Array.join()
const solution = (my_string) => console.log( [...my_string].filter((v, i) => my_string.indexOf(v) === i).join("") );
indexOf()는 배열에서 중복이있을경우, 첫번에 인덱스만 추출한다.
반응형'► JS Algorithm > Programmers' 카테고리의 다른 글
[Programmers] JavaScript 알고리즘 | Lv.0 수열과 구간 쿼리 1 (1) 2024.01.10 [Programmers] JavaScript 알고리즘 | Lv.0 등차수열의 특정한 항만 더하기 (0) 2024.01.10 [Programmers] JavaScript 알고리즘 | Lv.0 인덱스 바꾸기 (0) 2024.01.09 [Programmers] JavaScript 알고리즘 | Lv.0 주사위의 개수 (0) 2024.01.09 [Programmers] JavaScript 알고리즘 | Lv.0 가까운 1 찾기 (0) 2024.01.09