반응형

배열 정렬

Arrays.sort();

배열 비교

Arrays.mismatch();

import java.util.*;

class Solution {
    public String solution(String[] participant, String[] completion) {
        Arrays.sort(participant);
        Arrays.sort(completion);
        int mismatchIndex = Arrays.mismatch(participant,completion);
        return participant[mismatchIndex];
        
    }
}

 

다른방법

맵의 key-value에 접근하는법

Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

 

맵의 모든 key 가져오기

 Set<String> keys = map.keySet();

 

맵의 모든 value 가져오기

Collection<String> values = map.values();

 

 

import java.util.*;

class Solution {
    public String solution(String[] participant, String[] completion) {
        HashMap<String,Integer> particimap = new HashMap<>();
        for(String name : participant){
            int cnt = particimap.getOrDefault(name,0);
            particimap.put(name,++cnt);
        }
        
        for(String name : completion){
            int cnt = particimap.get(name);
            particimap.put(name,--cnt);
        }
        
        return particimap.entrySet()
            .stream()
            .filter(entry -> entry.getValue() >0)
            .map(Map.Entry::getKey)
            .findFirst()
            .orElse("없음");
    }
}

 

 

반응형

'코딩 관련 > 코딩문제풀기' 카테고리의 다른 글

[프로그래머스] 키패드 누르기  (0) 2024.10.09
[프로그래머스] 문자열 나누기  (0) 2024.10.06
[프로그래머스] 숫자 짝꿍  (0) 2024.10.04
[JAVA] 9012번 괄호  (0) 2023.03.13
[JAVA] 너의 평점은  (0) 2023.03.12

+ Recent posts