[JavaScript] 신고 메일 횟수

똔의 기록·2022년 5월 10일
0

JavaScript

목록 보기
2/14
post-thumbnail

우선 중복되는 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]);
}
profile
Keep going and level up !

0개의 댓글