공부기록/프로그래머스

[프로그래머스] 튜플

메델 2024. 2. 2. 23:29
import java.util.*;

class Solution {
    public List<Integer> solution(String s) {
 
        HashMap<Integer, Integer> map = new HashMap<>();
        String[] numbers = s.replaceAll("\\{","").replaceAll("\\}", "").split(",");
        
        for(String x: numbers){
            int num = Integer.parseInt(x.trim());
            map.put(num, map.getOrDefault(num, 0)+1);
        }
        
        List<Integer> keys = new ArrayList<>(map.keySet());
        
        Collections.sort(keys, (v1, v2) -> (map.get(v2).compareTo(map.get(v1))));
        
        ArrayList<Integer> list = new ArrayList<>();
        
        for(Integer key : keys){
           list.add(key);
        }
        

        return list;
    }
}