• + 0 comments

    Elixir solution (even though it feels not very pretty...):

    defmodule Solution do
        def split_numbers(s) do
            String.split(s, " ")
            |> Enum.map(fn x -> Integer.parse(x) |> elem(0) end)
        end
        
        def find_by_k(list, k) do
            grouped = Enum.group_by(list, fn x -> x end)
            output = Enum.filter(list, fn x -> Enum.count(grouped[x]) >= k end)
            |> Enum.uniq
            if Enum.count(output) == 0 do
                [-1]
            else
                output
            end
        end
    end
    
    
    
    input = IO.read(:stdio, :all)
      |> String.split("\n")
      
    [_ | elements] = input
    Enum.chunk_every(elements, 2)
    |> Enum.map(fn [config, list] -> 
        [_, k] = Solution.split_numbers(config)
        l = Solution.split_numbers(list)
        Solution.find_by_k(l, k)
    end)
    |> Enum.map(fn line ->
        IO.puts Enum.join(line, " ")
    end)