-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter-elements.hs
More file actions
35 lines (32 loc) · 1.16 KB
/
filter-elements.hs
File metadata and controls
35 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- Author: btjanaka (Bryon Tjanaka)
-- Problem: (HackerRank) filter-elements
-- Title: Filter Elements
-- Link: https://www.hackerrank.com/challenges/filter-elements/problem
-- Idea: (implementation)
-- Difficulty: easy
-- Tags: fp, recursion
import Control.Monad
import qualified Data.IntMap.Strict as IntMap
import Data.List.Split
import Data.Maybe
filterElements :: [Int] -> Int -> IntMap.IntMap Int -> [Int] -> [Int]
-- Do the actual filtering in the base case
filterElements [] k counts reversedNums =
reverse $ filter (\num -> counts IntMap.! num >= k) reversedNums
-- Simply add elements here
filterElements (x : xs) k counts reversedNums = filterElements
xs
k
(IntMap.insertWith (+) x 1 counts)
(if isNothing $ IntMap.lookup x counts then x : reversedNums else reversedNums
)
lineToInts :: String -> [Int]
lineToInts s = map read $ splitOn " " s
main = do
t <- readLn :: IO Int
forM_ [1 .. t] $ \i -> do
params <- getLine
a <- getLine
let [n, k] = lineToInts params
result = filterElements (lineToInts a) k IntMap.empty []
putStrLn $ if null result then "-1" else unwords $ map show result