• + 0 comments

    Here is a haskell solution...

    {-# LANGUAGE LambdaCase #-}
    
    import Data.List
    
    count e [] = 0
    count e (x : xs) = if x == e then 1 + count e xs else count e xs
    
    byCount c l = nub $ filter (\e -> count e l >= c) l
    
    readAllInts :: String -> [[Int]]
    readAllInts = map (map read . words) . lines
    
    showResults :: Show a => [[a]] -> String
    showResults = unlines . map (unwords . map show)
    
    takes c [] = []
    takes c l = take c l : takes c (drop c l)
    
    solve s = showResults $ map (\l -> if null l then [-1] else l) solutions
      where
        inputs = takes 2 . tail . readAllInts $ s
        solutions = map (\case [[_, c], l] -> byCount c l; _ -> []) inputs
        
    main = interact solve