나의 JS 코드
첫번째 시도
function solution(s) {
var answer = [];
let s_array = [];
let number = "0123456789";
let num_arr = [];
let num ='';
for(let i = 0 ; i< s.length-1 ; i++){
if( s[i] === '{'){
num_arr = [];
}
else if(number.indexOf(s[i])>=0 ){
num += s[i];
if(s[i+1] === ',' || s[i+1] === '}'){
num_arr.push(parseInt(num));
num = '';
}
}
else if (s[i] === '}' && (s[i+1] === ',' || i+1 === s.length-1))
s_array.push(num_arr);
}
s_array = s_array.sort((a,b)=> a.length - b.length);
for(let arr of s_array){
for(let number of arr){
if(answer.indexOf(number) === -1)
answer.push(number);
}
}
return answer;
}
두번째 시도
function solution(s) {
let answer = [];
s = s.replace("{{", "").replace("}}", "").split("},{").sort((a, b) => a.length - b.length);
s.forEach((data) => {
data = data.split(/[^0-9]/).map((strNumber) => parseInt(strNumber));
answer.push(...data.filter((num) => !answer.includes(num)))
})
return answer;
}