function solution(n,r) {
let answer=[];
let ch=Array.from({ length: n },() => 0);
let temp=Array.from({ length: r },() => 0);
function DFS(L) {
if(L===r) {
answer.push(Array.from(temp));
return;
} else {
for(let i=0; i<=n; ++i) {
if(ch[i]===0) {
ch[i]=1;
temp[L]=i+1;
DFS(L+1);
ch[i]=0;
}
}
}
}
DFS(0);
answer.map(v=>{
return v.sort((a,b)=>{
return a-b;
})
})
let result = [...new Set(answer.join('!').split('!'))].map(v=>{return v.split(',')}).map(v=>{return v.map(x=>x*1)});
return result;
}
console.log(solution(4,2));