Last active
April 27, 2016 13:25
-
-
Save JmeHsieh/c4f45651389d020409e1bb410c3c9cfa to your computer and use it in GitHub Desktop.
Solution for [Filter Elements] - https://www.hackerrank.com/challenges/filter-elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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