Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a list of lists has two or more identical elements

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.

like image 378
Tyler Joe Avatar asked Jan 27 '23 00:01

Tyler Joe


1 Answers

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.

like image 148
FrownyFrog Avatar answered Feb 01 '23 10:02

FrownyFrog