완주하지 못한 선수
function solution(participants, completions) {
const map = new Map();
for(const participant of participants) {
map.set(participant, (map.get(participant) || 0) + 1);
}
for(const completion of completions) {
map.set(completion, map.get(completion) - 1);
}
for(const [key, value] of map.entries()){
if(value === 1) return key;
}
}
폰켓몬
function solution(nums) {
const map = new Map();
for(const num of nums) {
map.set(num, (map.get(num) || 0) + 1);
}
return map.size >= nums.length / 2 ? nums.length / 2 : map.size;
}
위장
function solution(clothes) {
const map = new Map();
let answer = 1;
clothes.forEach(v => {
map.set(v[1], (map.get(v[1]) || 0) + 1);
})
for(const value of map.values()){
answer *= (value + 1);
}
return answer - 1;
}
베스트앨범
function solution(genres, plays) {
const answer = [];
const map = new Map();
genres.forEach((genre, index) => {
map.set(genre, (map.get(genre) || 0) + plays[index]);
})
const list = [...map.entries()].sort((a, b) => b[1] - a[1]);
list.forEach((value, index) => {
const play = plays.map((v, i) => [v, i])
.filter((v, i) => genres[i] === value[0])
.sort((a, b) => b[0] - a[0])
.slice(0, 2);
answer.push(...play.map(v => v[1]));
})
return answer;
}