https://programmers.co.kr/learn/courses/30/lessons/67258
function solution(gems) {
const count = new Set(gems).size;
let map = new Map();
let answer = [];
for (let i = 0; i < gems.length; i++) {
map.delete(gems[i]);
map.set(gems[i], i);
if (map.size === count) {
answer.push([map.values().next().value + 1, i + 1]);
}
}
answer.sort((a, b) => {
if (a[1] - a[0] === b[1] - b[0]) {
return a[1] - b[1];
}
return a[1] - a[0] - (b[1] - b[0]);
});
return answer[0];
}
Map 생성자를 처음 써봤는데 매우 유용했다.