수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다.
마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.
| participant | completion | return |
|---|---|---|
| ["leo", "kiki", "eden"] | ["eden", "kiki"] | "leo" |
| ["marina", "josipa", "nikola", "vinko", "filipa"] | ["josipa", "filipa", "marina", "nikola"] | "vinko" |
| ["mislav", "stanko", "mislav", "ana"] | ["stanko", "ana", "mislav"] | "mislav" |
예제 #1
"leo"는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #2
"vinko"는 참여자 명단에는 있지만, 완주자 명단에는 없기 때문에 완주하지 못했습니다.
예제 #3
"mislav"는 참여자 명단에는 두 명이 있지만, 완주자 명단에는 한 명밖에 없기 때문에 한명은 완주하지 못했습니다.
import java.util.*;
class Solution {
public String solution(String[] participant, String[] completion) {
Map<String, Integer> m = new HashMap<>();
for (int i = 0; i < participant.length; i++) {
if (m.containsKey(participant[i])) {
int n = m.get(participant[i]);
m.put(participant[i], n + 1);
} else {
m.put(participant[i], 1);
}
}
for (int i = 0; i < completion.length; i++) {
if (m.containsKey(completion[i])) {
int n = m.get(completion[i]);
m.put(completion[i], n - 1);
}
}
String answer = null;
for (Map.Entry<String, Integer> entry : m.entrySet()) {
if(entry.getValue() != 0){
answer = entry.getKey();
}
}
return answer;
}
}