Untitled

 avatar
unknown
plain_text
3 years ago
1.1 kB
4
Indexable
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        HashMap<String, Integer> mapita = new HashMap<>();
        PriorityQueue<Map.Entry<String,Integer>> RepW = new PriorityQueue<>((a,b)->{
            if((a.getValue()-b.getValue()) != 0){
                return b.getValue()-a.getValue();
            }else{
              return  a.getKey().compareTo(b.getKey());
            }
        });      
        int N = read.nextInt();
        int K = read.nextInt();
        
        while(read.hasNext()){
            String llave= read.next();
        if(mapita.isEmpty() || mapita.containsKey(llave)==false){
            mapita.put(llave, 1);
        }
        else if(mapita.containsKey(llave)==true){
            mapita.put(llave, mapita.get(llave)+1);
        }}

        for(Map.Entry<String,Integer> e: mapita.entrySet()){RepW.add(e);}
        
        for(int i=0; i<K; i++){
            System.out.print(RepW.poll().getKey() + " ");
        }

        }
}
Editor is loading...