-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmostRepeatedElem.hs
39 lines (33 loc) · 924 Bytes
/
mostRepeatedElem.hs
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
36
37
38
39
-- http://codepad.org/QollsjCw
import Control.Monad
import Data.Function
import Data.List
import Test.QuickCheck
-- $setup
-- >>> import Control.Applicative ((<*>))
-- >>> import Data.List (isInfixOf)
-- >>> import Test.QuickCheck
-- Level: Easy
-- Pointfree: yes
-- | mostRepeatedElem
-- Returns the element with the longest (consecutive) repetition and the
-- repetition number
-- If there are tie, the last most repeated element is returned
-- It returns error on empty string
--
-- Examples:
--
-- >>> mostRepeatedElem "hello world!"
-- ('l',2)
--
-- >>> mostRepeatedElem [1,1,2,2]
-- (2,2)
--
-- prop> (flip isInfixOf <*> uncurry (flip replicate) . mostRepeatedElem) . getNonEmpty
mostRepeatedElem :: Eq a => [a] -> (a,Int)
mostRepeatedElem =
maximumBy (compare `on` snd) .
map (liftM2 (,) head length) . -- also map (head &&& length)
group
main = do
print $ mostRepeatedElem "Hello!!!1one"