I need to write a function which checks if a list has two or more same elements and returns true or false.
For example [3,3,6,1]
should return true, but [3,8]
should return false.
Here is my code:
identical :: [Int] -> Bool
identical x = (\n-> filter (>= 2) n )( group x )
I know this is bad, and it does not work. I wanted to group the list into list of lists, and if the length of a list is >= 2, then it is should return with true otherwise false.
Use any
to get a Bool result.
any ( . . . ) ( group x )
Don’t forget to sort the list, group
works on consecutive elements.
any ( . . . ) ( group ( sort x ) )
You can use (not . null . tail) for a predicate, as one of the options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With