[프로그래머스] 완주하지 못한 선수-해시 Level1

[프로그래머스] 완주하지 못한 선수-해시 Level1

- 풀이

동명이이인이 있을수 있다.

참가자들을 맵에 넣고

완주자들을 맵에서 뺀후

value가 0 이 아닌 키값을 출력했다.

import java.util.HashMap; class Solution { public String solution(String[] participant, String[] completion) { String answer = ""; HashMap map = new HashMap<>(); for(String x : participant) { map.put(x, map.getOrDefault(x, 0)+1); } for(String x : completion) { map.put(x, map.get(x)-1); } for(String x : map.keySet()) { if(map.get(x)!=0) answer=x; } return answer; } }

from http://rotomoo.tistory.com/17 by ccl(A) rewrite - 2021-11-01 15:01:42