프로그래머스 LEVEL 1(신고 결과 받기))

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2022-08-02

  • 해결 방법 :

    • Set으로 변경 후 list로 다시 반환하여 report속 중복 신고 제거
    • 신고 당한 사람 별 신고자를 배열에 추가하여 카운팅
  • 회고 : new Map()을 활용하여 set(), get() 함수를 사용하는 방법 생각하기

  • 코드

  function solution(id_list, report, k) {
      var answer = [];
      report = [...new Set(report)];
      
      const countDict = {};
      var obj = {};
      id_list.map(id => {
          Object.assign(obj, {[id]: []});
          Object.assign(countDict, {[id]: 0});
      });
      for (const temp of report) {
          const list = temp.split(' ');
          const [reporter, reportee] = list;
          obj[reportee].push(reporter);
      }
      
      for (const reportee of id_list) {
          if(obj[reportee].length >= k) {
              for (const reporter of obj[reportee]) {
                  countDict[reporter] += 1;
              }
          }
      }
      for (const key in countDict) {
          answer.push(countDict[key]);
      }
      return answer;
  }