[JAVA] 해시_완주하지 못한 선수(Level 1)

EunBi Na·2023년 4월 19일
0

링크텍스트

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public String solution(String[] participant, String[] completion) {
        Map<String, Integer> count = new HashMap<>();
            
        for (String name : participant) {
            count.putIfAbsent(name, 0);
            count.put(name, count.get(name) + 1);
        }
        
        for (String name : completion) {
            int v = count.get(name) - 1;
            count.put(name, v);
            if (v == 0) count.remove(name);
        }
        return count.keySet().iterator().next();
    }
}

keySet()를 호출하면 Map에 담긴 key들을 Set형태로 반환,
이를 순회하기 위해 iterator() 메서드를 호출하고,
첫 번째 원소를 가져오기 위해 next()를 호출하면 된다.

profile
This is a velog that freely records the process I learn.

0개의 댓글