HackerRank - Filter huge list for elements that occur more than a given number of times
It seems that the suggestion I got in the comment is enough to pass the HR test:
import Data.Maybe (fromJust)
import Data.List (foldl', nub)
import qualified Data.Map as M

main :: IO()
main = do
  nCasesStr &lt;- getLine
  let nCase = read nCasesStr :: Int
  sequence_ $ replicate nCase $ do
    k    &lt;- (str2int. last . words) &lt;$&gt; getLine
    list &lt;-    map str2int . words  &lt;$&gt; getLine
    let outList = processList k list
    if null outList
      then putStr $ show (-1)
      else mapM_ (putStr . (++&quot; &quot;) . show) outList
    putStrLn &quot;&quot;
    where
      str2int = read :: String -&gt; Int

processList :: Int -&gt; [Int] -&gt; [Int]
processList k = nub . (filter &lt;$&gt; (flip (\x -&gt; (k &lt;=) . fromJust . M.lookup x . list2map)) &lt;*&gt; id)
  where
    list2map = foldl' step M.empty
    step map num = M.insertWith (+) num 1 map