코딩테스트/LV. 0
LV. 0 :: 중복된 문자 제거
KUROMI98
2023. 1. 26. 02:29
https://school.programmers.co.kr/learn/courses/30/lessons/120888
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
function solution(my_string)
{
var arr = my_string.split('');
// "people" 을 ["p","e","o","p","l","e"] 로 바꿔준다.
const newarr = arr.filter( (wonso, index) => arr.indexOf(wonso) === index );
// ["p","e","o","p","l","e"] 를 ["p","e","o","l"] 로 바꿔준다.
// 원리: indexOf 함수는 같은 값을 가진 원소들 중 맨 처음에 등장한 원소만의 인덱스를 추출한다.
// arr 내에서 원소의 index가 true가 되는 경우는 맨 처음에 등장한 원소 하나뿐이다.
// filter는 true일때는 원소를 제거하지 않고 false일 때는 원소를 제거하기 때문에,
// 맨 처음에 등장한 원소랑 같은 값을 가진 것들은 맨 처음에 등장한 원소 하나 빼고 다 제거된다.
var answer = newarr.join('');
// ["p","e","o","l"] 을 "peol" 로 바꿔준다.
return answer;
}
+)230516
const solution = (my_string) => {
return my_string.split('').filter((x,index) => my_string.split('').indexOf(x)===index).join('');
}
230603 python
def solution(my_string):
arr = list(my_string)
unique_arr = [x for i, x in enumerate(arr) if x not in arr[:i]]
return ''.join(unique_arr)
230916 C++
#include <string>
#include <vector>
using namespace std;
string solution(string my_string) {
string answer = "";
for (char c : my_string) {
if (answer.find(c) == string::npos) {
answer += c;
}
}
return answer;
}