import java.util.Arrays;
class Solution {
public String solution(String[] participant, String[] completion) {
Arrays.sort(participant);
Arrays.sort(completion);
int i;
for(i = 0; i < completion.length; i++){
if(!participant[i].equals(completion[i])){
return participant[i];
}
}
return participant[i];
}
}
import java.util.HashMap;
class Solution {
public String solution(String[] participant, String[] completion) {
String result = "";
HashMap<String, Integer> hashMap = new HashMap<>();
for(String p : participant){
hashMap.put(p, hashMap.getOrDefault(p, 0) + 1);
}
for(String c : completion){
hashMap.put(c, hashMap.get(c) - 1);
}
for(String key : hashMap.keySet()){
if(hashMap.get(key) != 0){
result = key;
break;
}
}
return result;
}
}
getOrDefault(Object key, V defaultValue) : 찾는 key가 존재한다면 찾는 key의 value을 반환하고 없다면 기본 value을 반환
String[] arr1 = {"qwe", "asd", "zxc", "qwe"};
String[] arr2 = {"qwe", "asd", "zxc"};
Map<String, Integer> map = new HashMap<>();
for(String a : arr1) map.put(a, map.getOrDefault(a, 0) + 1);
System.out.println(map);
// {qwe=2, asd=1, zxc=1}
문제 출처 : https://programmers.co.kr/learn/courses/30/lessons/42576