우선 중복되는 id 신고값을 없애주기 위해서 이전 문제에서도 썼던 new Set(array)를 사용!
필요한 항목인 id별 신고받은 횟수, id별 신고자, 처리 email 횟수에 대한 객체를 선언하고 초기화해주고 풀었다.
function solution(id_list, report, k) {
const reportedCnt ={};
const reportedBy ={};
const mailCnt ={};
// report 중복제거
const reportedSet = new Set(report);
//id 당 신고당한 횟수, 신고자, 메일 횟수 초기화
id_list.forEach((element)=>{
reportedCnt[element] =0;
reportedBy[element]=[];
mailCnt[element]=0;
});
// reportedSet에서 split 후 횟수, 신고자 저장
reportedSet.forEach((element)=>{
const [id, reported] = element.split(" ");
reportedCnt[reported] += 1;
reportedBy[reported].push(id);
});
// k회 이상 신고 당한 사람 신고자에게 email 날리기
for(const reportedId in reportedCnt){
if(reportedCnt[reportedId]>=k){
reportedBy[reportedId].forEach((reporter)=>{
mailCnt[reporter] +=1;
});
}
}
return id_list.map((id)=> mailCnt[id]);
}