프로그래머스 LEVEL 1(튜플)
-
사용 언어 : javascript
-
해결 날짜 : 2022-08-24
-
해결 방법 :
- 정규 표현식을 통해 튜플 속 {, } 제거 후 집합 별 분리
- 길이 오름차순으로 정렬 후 문자열형 배열을 숫자형 배열로 변환
- 길이가 작은 순서대로 현재 정답 배열에 들어가 있지 않은 값들만 추가
-
회고 : JSON.parse 사용하기, Set() 사용하기
-
코드
function solution(s) { var answer = []; const regex = /{|}}/g; const splitted = s.replace(regex, '').split('},'); splitted.sort((a, b) => a.length - b.length); for (const [index, value] of splitted.entries()) { splitted[index] = value.split(',').map((v) => { return parseInt(v, 10); }); } for (const val of splitted) { var temp = val.filter((v) => { return !answer.includes(v); }); answer.push(temp.pop()); } return answer; }
-
출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges