[Programmers] 해시 - 완주하지 못한 선수

zzenee·2022년 4월 28일
0

Algorithm&Coding-test

목록 보기
4/30
post-thumbnail

https://programmers.co.kr/learn/courses/30/lessons/42576

Problem

Code

import java.util.*;
class Solution {
    public String solution(String[] participant, String[] completion) {
        String answer = "";
        HashMap<String, Integer> map = new HashMap<>();
        
        // 참가자 명단 구성
        for(String s: participant) {
            map.put(s, map.getOrDefault(s, 0) + 1);
        }
        
        // 참가자 명단에서 완주자 확인
        for (String str : completion) {
            map.put(str, map.get(str) - 1);
            if (map.get(str) == 0) {
                map.remove(str);
            }
        }
        
        // 완주하지 못한 선수 탐색
        for (String key: map.keySet()) {
            answer = key;
        }
        return answer;
    }
}
profile
꾸준히

0개의 댓글