Skip to content

Instantly share code, notes, and snippets.

@JmeHsieh
Last active April 27, 2016 13:25
Show Gist options
  • Select an option

  • Save JmeHsieh/c4f45651389d020409e1bb410c3c9cfa to your computer and use it in GitHub Desktop.

Select an option

Save JmeHsieh/c4f45651389d020409e1bb410c3c9cfa to your computer and use it in GitHub Desktop.
module hackerrank where
import Data.List
orderedElem :: [Int] -> [Int]
orderedElem = foldl (\r x -> if x `elem` r then r else r++[x]) []
unorderedGrouping :: [Int] -> [[Int]]
unorderedGrouping = group . sort
orderedGrouping :: [Int] -> [[Int]]
orderedGrouping = (\(es, gs) -> (es >>= (\e -> filter (\(x:_) -> x == e) gs))) . apply2
where apply2 = (,) <$> orderedElem <*> unorderedGrouping
result :: [Int] -> Int -> [Int]
result a k = check $ filter ((>= k) . length) $ orderedGrouping a
where check n | n == [] = [-1]
| otherwise = map head n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment